-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.js
58 lines (46 loc) · 1.4 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
51
52
53
54
55
56
57
58
require("babel-register");
const express = require("express");
const React = require("react");
const ReactRouter = require("react-router-dom");
const _ = require("lodash");
const fs = require("fs");
const cors = require("cors");
const Index = require("./js/Index").default;
const StaticRouter = ReactRouter.StaticRouter;
const port = 8080;
const baseTemplate = fs.readFileSync("./index.html");
const template = _.template(baseTemplate);
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const webpack = require("webpack");
const config = require("./webpack.config");
const compression = require("compression");
const server = express();
server.use(cors())
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 = React.createElement(
StaticRouter,
{ location: req.url, context },
React.createElement(Index)
)
if (context.url) {
res.redirect(context.url);
}
res.write(template({ body }));
res.end();
});
server.listen(process.env.PORT || port, () => {
console.log(`listening on ${port}`);
});