Created
May 5, 2016 12:09
-
-
Save jaseflow/f19d218cd2fd409351496bf96ce35021 to your computer and use it in GitHub Desktop.
server.js
This file contains hidden or 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
import express from 'express'; | |
import config from '../../config/config'; | |
let accessLogger = require("../../config/logs")(config); | |
import appVersion from '../../config/version'; | |
import webpack from 'webpack'; | |
import webpackConfig from '../../webpack.config'; | |
import webpackDevMiddleware from 'webpack-dev-middleware'; | |
import webpackHotMiddleware from 'webpack-hot-middleware'; | |
import React from 'react'; | |
import { RoutingContext, match } from 'react-router'; | |
import { Provider } from 'react-redux'; | |
import createLocation from 'history/lib/createLocation'; | |
import { fetchComponentDataBeforeRender } from '../common/api/fetchComponentDataBeforeRender'; | |
import configureStore from '../common/store/configureStore'; | |
import { getUser } from '../common/api/user'; | |
import routes from '../common/routes'; | |
import packagejson from '../../package.json'; | |
const app = express(); | |
const renderFullPage = (html, initialState) => { | |
return ` | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Primer - Create, deploy, monitor</title> | |
<link rel="stylesheet" type="text/css" href="/static/styles.css"> | |
<link rel="shortcut icon" href="https://s3.amazonaws.com/uploads.hipchat.com/28219/1858130/6xFg0kp1zwblk48/favicon2x.png"> | |
</head> | |
<body> | |
<div id="root">${html}</div> | |
<script> | |
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}; | |
</script> | |
<script src="/static/bundle.js"></script> | |
</body> | |
</html> | |
`; | |
}; | |
if(config.name !== "Production"){ | |
const compiler = webpack(webpackConfig); | |
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: webpackConfig.output.publicPath })); | |
app.use(webpackHotMiddleware(compiler)); | |
}else{ | |
app.use('/static', express.static(__dirname + '/../../dist')); | |
} | |
if(!process.browser){ | |
//Log All Access | |
app.use(accessLogger.logAllAccess); | |
} | |
// PRIMER ROUTES | |
app.get("/isActive", function(req, res){ | |
res.setHeader('cache-control', 'no-cache'); | |
res.status(200).send('ACTIVE'); | |
}); | |
app.get("/buildInfo", function(req, res){ | |
res.setHeader('cache-control', 'no-cache; max-age=0'); | |
res.setHeader('content-type', 'application/json'); | |
res.status(200).send(JSON.stringify(appVersion)); | |
}); | |
app.get('/*', function (req, res) { | |
const location = createLocation(req.url); | |
getUser(user => { | |
if(!user) { | |
return res.status(401).end('Not Authorised'); | |
} | |
match({ routes, location }, (err, redirectLocation, renderProps) => { | |
if(err) { | |
console.error(err); | |
return res.status(500).end('Internal server error'); | |
} | |
if(!renderProps) | |
return res.status(404).end('Not found'); | |
const store = configureStore({user : user, version : packagejson.version}); | |
const InitialView = ( | |
<Provider store={store}> | |
{() => | |
<RoutingContext {...renderProps} /> | |
} | |
</Provider> | |
); | |
//This method waits for all render component promises to resolve before returning to browser | |
fetchComponentDataBeforeRender(store.dispatch, renderProps.components, renderProps.params) | |
.then(html => { | |
const componentHTML = React.renderToString(InitialView); | |
const initialState = store.getState(); | |
res.status(200).end(renderFullPage(componentHTML,initialState)); | |
}) | |
.catch(err => { | |
console.log(err); | |
res.end(renderFullPage("",{})); | |
}); | |
}); | |
} | |
); | |
}); | |
const server = app.listen(config.port, function () { | |
const host = ( server.address().address === "::" ) ? "localhost" : server.address().address; | |
const port = server.address().port; | |
console.log('React redux app listening at http://%s:%s', host, port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment