-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (144 loc) · 4.64 KB
/
server.js
File metadata and controls
164 lines (144 loc) · 4.64 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import express from 'express';
import React from 'react';
import { match, createMemoryHistory, RouterContext } from 'react-router';
import { renderToString } from 'react-dom/server';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { trigger } from 'redial';
// Routes
import Routes from './src/common/components/Routes';
import { configureStore } from './src/common/store';
const app = express();
/************************************************************
*
* Express routes for:
* - app.js
* - style.css
* - index.html
*
************************************************************/
// Serve application file depending on environment
app.get('/app.js', (req, res) => {
if (process.env.PRODUCTION) {
res.sendFile(__dirname + '/build/app.js');
} else {
res.redirect('//' + process.env.DEV_HOST + ':' + process.env.DEV_PORT + '/build/app.js');
}
});
// Serve aggregate stylesheet depending on environment
app.get('/style.css', (req, res) => {
if (process.env.PRODUCTION) {
res.sendFile(__dirname + '/build/style.css');
} else {
res.redirect('//' + process.env.DEV_HOST + ':' + process.env.DEV_PORT + '/build/style.css');
}
});
const renderFullPage = (html, initialState, serverMessages) => {
let env = {
TODO_API_ENDPOINT: process.env.TODO_API_ENDPOINT
};
return `
<!DOCTYPE html>
<html>
<head>
<title>React Todo Web</title>
<link rel="stylesheet" href="/style.css" charset="utf-8">
</head>
<body>
<div id="app">${html}</div>
<script>
window.INITIAL_STATE = ${JSON.stringify(initialState)};
window.ENV = ${JSON.stringify(env)};
window.SERVER_MESSAGES = ${JSON.stringify(serverMessages)};
</script>
<script src="/app.js"></script>
</body>
</html>
`;
};
// Serve index page
app.get('*', (req, res) => {
const history = createMemoryHistory(req.path);
match({ routes: Routes, location: req.url, history: history }, (err, redirect, renderProps) => {
if (err) {
res.status(500).send(err.message);
} else if (redirect) {
res.redirect(302, redirect.pathname + redirect.search);
} else if (!renderProps) {
res.status(404).send('Not found');
} else if (!process.env.SERVER_RENDERING) {
res.send(renderFullPage(""));
} else {
const serverMessages = [];
global.alert = (message) => { if (message) { serverMessages.push(message.toString()); } }
const store = configureStore();
const { dispatch } = store;
const { components } = renderProps;
// Define locals to be provided to all lifecycle hooks:
const locals = {
path: renderProps.location.pathname,
query: renderProps.location.query,
params: renderProps.params,
// Allow lifecycle hooks to dispatch Redux actions:
dispatch,
};
// Uncomment here if saga is required in server-side rendering
const createSaga = require('./src/common/createSaga').default;
store.runSaga(createSaga(store.getState)).done.then(() => {
const html = renderToString((
<Provider store={store}>
<RouterContext {...renderProps}/>
</Provider>
));
res.send(renderFullPage(html, store.getState(), serverMessages));
}).catch((e) => {
console.log(e.message)
res.status(500).send(e.message)
});
trigger('fetch', components, locals).then(() => {
store.close();
})
.catch((e) => {
console.log(e.message)
res.status(500).send(e.message)
});
}
});
});
/*************************************************************
*
* Webpack Dev Server
*
* See: http://webpack.github.io/docs/webpack-dev-server.html
*
*************************************************************/
if (!process.env.PRODUCTION) {
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.local.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
noInfo: true,
historyApiFallback: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000 // see https://github.com/webpack/webpack-dev-server/issues/155
},
}).listen(process.env.DEV_PORT, '::', (err, result) => {
if (err) {
console.log(err);
}
});
}
/******************
*
* Express server
*
*****************/
const port = process.env.PORT || 8080;
const server = app.listen(port, () => {
const host = server.address().address;
const port = server.address().port;
console.log('Essential React listening at http://%s:%s', host, port);
});