-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/using hapi #8
Open
koushik0004
wants to merge
8
commits into
andreiduca:react-router
Choose a base branch
from
koushik0004:feature/using-hapi
base: react-router
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
909ae54
updated dependencies; fixed server app to work with babel 7
andreiduca ee844bf
updated all the outdated packages in application
sapientkoushik0004 0304326
updating some other required stuffs, setting up
sapientkoushik0004 a7cb181
made client side part as jsx
sapientkoushik0004 df09e8c
initial request handling completed with hapi js
sapientkoushik0004 931f13e
update in redirect method
sapientkoushik0004 0cd2703
initial update for bootstrap css
sapientkoushik0004 05b4b90
updating css to scss and adding bootstrap
sapientkoushik0004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
require('ignore-styles'); | ||
require('url-loader'); | ||
require('file-loader'); | ||
require('babel-register')({ | ||
require('@babel/register')({ | ||
ignore: [ /(node_modules)/ ], | ||
presets: ['es2015', 'react-app'], | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
plugins: [ | ||
'syntax-dynamic-import', | ||
'dynamic-import-node', | ||
'react-loadable/babel' | ||
] | ||
}); | ||
require('./index'); | ||
// require('./index'); | ||
require('./index-hapi'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import 'babel-polyfill'; | ||
import hapi from '@hapi/hapi'; | ||
import Inert from '@hapi/inert'; | ||
import path from "path"; | ||
|
||
import serverRenderer from '../middleware/renderer'; | ||
import configureStore from '../../src/store/configureStore'; | ||
|
||
const router = express.Router(); | ||
|
||
const init = async () => { | ||
|
||
const server = hapi.server({ | ||
port: 1030, | ||
host: 'localhost' | ||
}); | ||
|
||
await server.register(Inert); | ||
|
||
// Serve static files | ||
server.route({ | ||
method: 'GET', | ||
path: '/{filepath*}', | ||
config: { | ||
auth: false, | ||
// cache: { | ||
// expiresIn: 24 * 60 * 60 * 1000, | ||
// privacy: 'public' | ||
// } | ||
}, | ||
handler: { | ||
directory: { | ||
path: path.join(__dirname, '../../build'), | ||
listing: false, | ||
index: false | ||
} | ||
} | ||
}); | ||
|
||
server.route({ | ||
method: 'GET', | ||
path:'/', | ||
handler: (request, reply) => { | ||
const context = { }; | ||
const jsx = ( | ||
<StaticRouter context={ context } location={ request.url }> | ||
<Layout /> | ||
</StaticRouter> | ||
); | ||
const reactDom = renderToString( jsx ); | ||
return htmlTemplate( reactDom ); | ||
} | ||
}); | ||
|
||
await server.start(); | ||
console.log('Server running on %s', server.info.uri); | ||
}; | ||
|
||
process.on('unhandledRejection', (err) => { | ||
console.log(err); | ||
process.exit(1); | ||
}); | ||
|
||
init(); | ||
|
||
const actionIndex = (req, res, next) => { | ||
const store = configureStore(); | ||
serverRenderer(store)(req, res, next); | ||
}; | ||
|
||
|
||
// root (/) should always serve our server rendered page | ||
router.use('^/$', actionIndex); | ||
|
||
// other static resources should just be served as they are | ||
router.use(express.static( | ||
path.resolve(__dirname, '..', '..', 'build'), | ||
{ maxAge: '30d' }, | ||
)); | ||
|
||
// any other route should be handled by react-router, so serve the index page | ||
router.use('*', actionIndex); | ||
|
||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'babel-polyfill'; | ||
import hapi from '@hapi/hapi'; | ||
import Inert from '@hapi/inert'; | ||
import path from "path"; | ||
import Loadable from 'react-loadable'; | ||
|
||
import serverRenderer from './middleware/renderer'; | ||
import configureStore from '../src/store/configureStore'; | ||
|
||
const init = async () => { | ||
|
||
const server = hapi.server({ | ||
port: 1030, | ||
host: 'localhost' | ||
}); | ||
|
||
await server.register(Inert); | ||
|
||
// Serve static files | ||
server.route({ | ||
method: 'GET', | ||
path: '/{filepath*}', | ||
config: { | ||
auth: false, | ||
cache: { | ||
expiresIn: 24 * 60 * 60 * 1000, | ||
privacy: 'public' | ||
} | ||
}, | ||
handler: { | ||
directory: { | ||
path: path.join(__dirname, '../build'), | ||
listing: false, | ||
index: false | ||
} | ||
} | ||
}); | ||
|
||
server.route([{ | ||
method: 'GET', | ||
path:'/', | ||
handler: async (request, reply) => { | ||
const store = configureStore(); | ||
const htmlToRender = await serverRenderer(store)(request, reply); | ||
if(!htmlToRender){ | ||
console.log(`Error occured!`); | ||
} | ||
console.log(`${JSON.stringify(request.params)}`); | ||
return htmlToRender | ||
} | ||
}, { | ||
method: 'GET', | ||
path:'/another', | ||
handler: async (request, reply) => { | ||
return reply.redirect('/'); | ||
// const store = configureStore(); | ||
// const htmlToRender = await serverRenderer(store)(request, reply); | ||
// if(!htmlToRender){ | ||
// console.log(`Error occured!`); | ||
// } | ||
// console.log(`${JSON.stringify(request.params)} - ${server.info.uri}`); | ||
// return htmlToRender | ||
} | ||
}]); | ||
|
||
await server.start(); | ||
console.log('Server running on %s', server.info.uri); | ||
|
||
}; | ||
|
||
process.on('unhandledRejection', (err) => { | ||
console.log(err); | ||
process.exit(1); | ||
}); | ||
|
||
Loadable.preloadAll().then(() => { | ||
init(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I don't see
StaticRouter
orLayout
in scope of this file...