-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (40 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
require('babel-register');
const express = require('express');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const ReactRouter = require('react-router');
const match = ReactRouter.match;
const RouterContext = ReactRouter.RouterContext;
const ReactRedux = require('react-redux');
const Provider = ReactRedux.Provider;
const Store = require('./redux/Store.jsx');
const store = Store.store;
const _ = require('lodash');
const fs = require('fs');
const port = process.env.PORT || 8080;
const baseTemplate = fs.readFileSync('./index.html');
const template = _.template(baseTemplate);
const App = require('./components/App.jsx');
const Routes = App.Routes;
const app = express();
app.use('/public', express.static('./public'));
app.use((req, res) => {
match({ routes: Routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
const body = ReactDOMServer.renderToString(
React.createElement(Provider, {store},
React.createElement(RouterContext, renderProps)
)
);
res.status(200).send(template({ body }));
} else {
res.status(404).send('Not found');
}
})
});
console.log('listening on port ' + port);
app.listen(port);