-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
109 lines (86 loc) · 3.5 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import compression from 'compression';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { match, RoutingContext } from 'react-router';
import { createStore, applyMiddleware, compose } from 'redux';
import { Provider } from 'react-redux';
import createLocation from 'history/lib/createLocation';
import createMemoryHistory from 'history/lib/createMemoryHistory';
import rootReducer from './app/reducers';
import middleware from './app/middleware';
import createRoutes from './app/routes/routes';
const app = express();
const port = process.env.PORT || 3000;
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(compression());
app.use(bodyParser.json());
app.use('/build', express.static(path.join(__dirname, 'build'))); //设置build文件夹为存放静态文件的目录
// app,use('/api', apiRoutes);
app.use(handleRender);
function handleRender(req, res) {
const history = createMemoryHistory();
const routes = createRoutes(history)
const location = createLocation(req.url);
// req.url is the full url
match({ routes, location }, (err, redirectLocation, renderProps) => {
if(err) {
return res.status(500).send(err.message)
}
if(!renderProps) {
return res.status(404).send('not found')
}
const store = compose(
applyMiddleware.apply(this, middleware)
)(createStore)(rootReducer)
// render the component to string
const initialView = renderToString(
<div>
<Provider store={store}>
{ <RoutingContext {...renderProps} /> }
</Provider>
</div>
)
const initialState = store.getState();
const assets = require('./stats.json');
res.render('index', {
html: initialView,
assets,
initialState
})
// res.status(200).send(renderFullPage(initialView, initialState))
})
}
// function renderFullPage(html, initialState) {
// const assets = require('./stats.json');
// return `
// <!DOCTYPE html>
// <!--[if lt IE 7 ]> <html lang="en" class="ie6" > <![endif]-->
// <!--[if IE 7 ]> <html lang="en" class="ie7" > <![endif]-->
// <!--[if IE 8 ]> <html lang="en" class="ie8" > <![endif]-->
// <!--[if IE 9 ]> <html lang="en" class="ie9" > <![endif]-->
// <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="" > <!--<![endif]-->
// <head>
// <meta charset="utf-8">
// <title>react-redux-router</title>
// <link href="./build/${assets.assetsByChunkName.app[1]}" rel="stylesheet">
// </head>
// <body>
// <div id="app">${html}</div>
// <script>
// window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}
// </script>
// <script src="http://cdn.bootcss.com/react/0.14.7/react.min.js"></script>
// <script src="http://cdn.bootcss.com/react/0.14.7/react-dom.min.js"></script>
// <script src="./build/${assets.assetsByChunkName.vendors}"></script>
// <script src="./build/${assets.assetsByChunkName.app[0]}"></script>
// </body>
// </html>
// `
// }
app.listen(port, () => {
console.log('this server is running on ' + port)
});