-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (43 loc) · 1.41 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* eslint no-console:0 */
require('babel-register');
const express = require('express');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const ReactRouter = require('react-router-dom');
const _ = require('lodash');
const fs = require('fs');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const compression = require('compression');
const webpack = require('webpack');
const config = require('./webpack.config');
const App = require('./js/App').default;
const StaticRouter = ReactRouter.StaticRouter;
const port = 8080;
const baseTemplate = fs.readFileSync('./index.html');
const template = _.template(baseTemplate);
const server = express();
server.use(compression());
if (process.env.NODE_ENV === 'development') {
const compiler = webpack(config);
server.use(
webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
})
);
server.use(webpackHotMiddleware(compiler));
}
server.use('/public', express.static('./public'));
server.use((req, res) => {
const context = {};
const body = ReactDOMServer.renderToString(
React.createElement(StaticRouter, { location: req.url, context }, React.createElement(App))
);
if (context.url) {
res.redirect(301, context.url);
}
res.write(template({ body }));
res.end();
});
console.log(`listening on ${port}`);
server.listen(port);