-
Notifications
You must be signed in to change notification settings - Fork 171
/
server.js
183 lines (159 loc) · 5.48 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import path from 'path';
import express from 'express';
import React from 'react';
import ReactDOM from 'react-dom/server';
import morgan from 'morgan';
import favicon from 'serve-favicon';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import httpProxy from 'http-proxy';
import PrettyError from 'pretty-error';
import http from 'http';
import { Router, StaticRouter } from 'react-router';
import { renderRoutes } from 'react-router-config';
import { createMemoryHistory } from 'history';
import Loadable from 'react-loadable';
import { getBundles } from 'react-loadable/webpack';
import { trigger } from 'redial';
import config from 'config';
import createStore from 'redux/create';
import apiClient from 'helpers/apiClient';
import Html from 'helpers/Html';
import routes from 'routes';
import { createApp } from 'app';
import { getChunks, waitChunks } from 'utils/chunks';
import asyncMatchRoutes from 'utils/asyncMatchRoutes';
import { Provider } from 'components';
const pretty = new PrettyError();
const chunksPath = path.join(__dirname, '..', 'static', 'dist', 'loadable-chunks.json');
process.on('unhandledRejection', (reason, p) => console.error('Unhandled Rejection at: Promise ', p, pretty.render(reason)));
const targetUrl = `http://${config.apiHost}:${config.apiPort}`;
const app = express();
const server = new http.Server(app);
const proxy = httpProxy.createProxyServer({
target: targetUrl,
ws: true
});
app
.use(morgan('dev', { skip: req => req.originalUrl.indexOf('/ws') !== -1 }))
.use(cookieParser())
.use(compression())
.use(favicon(path.join(__dirname, '..', 'static', 'favicon.ico')))
.use('/manifest.json', (req, res) => res.sendFile(path.join(__dirname, '..', 'static', 'manifest.json')));
app.use('/dist/service-worker.js', (req, res, next) => {
res.setHeader('Service-Worker-Allowed', '/');
res.setHeader('Cache-Control', 'no-store');
return next();
});
app.use(express.static(path.join(__dirname, '..', 'static')));
app.use((req, res, next) => {
res.setHeader('X-Forwarded-For', req.ip);
return next();
});
// Proxy to API server
app.use('/api', (req, res) => {
proxy.web(req, res, { target: targetUrl });
});
app.use('/ws', (req, res) => {
proxy.web(req, res, { target: `${targetUrl}/ws` });
});
server.on('upgrade', (req, socket, head) => {
proxy.ws(req, socket, head);
});
// added the error handling to avoid https://github.com/nodejitsu/node-http-proxy/issues/527
proxy.on('error', (error, req, res) => {
if (error.code !== 'ECONNRESET') {
console.error('proxy error', error);
}
if (!res.headersSent) {
res.writeHead(500, { 'content-type': 'application/json' });
}
const json = {
error: 'proxy_error',
reason: error.message
};
res.end(JSON.stringify(json));
});
app.use(async (req, res) => {
if (__DEVELOPMENT__) {
// Do not cache webpack stats: the script file would change since
// hot module replacement is enabled in the development env
webpackIsomorphicTools.refresh();
}
const providers = {
app: createApp(req),
client: apiClient(req)
};
const history = createMemoryHistory({ initialEntries: [req.originalUrl] });
const store = createStore({
history,
helpers: providers
});
function hydrate() {
res.write('<!doctype html>');
ReactDOM.renderToNodeStream(<Html assets={webpackIsomorphicTools.assets()} store={store} />).pipe(res);
}
if (__DISABLE_SSR__) {
return hydrate();
}
try {
const { components, match, params } = await asyncMatchRoutes(routes, req.path);
const triggerLocals = {
...providers,
store,
match,
params,
history,
location: history.location
};
await trigger('inject', components, triggerLocals);
await trigger('fetch', components, triggerLocals);
const modules = [];
const context = {};
const component = (
<Loadable.Capture report={moduleName => modules.push(moduleName)}>
<Provider store={store} {...providers}>
<Router history={history}>
<StaticRouter location={req.originalUrl} context={context}>
{renderRoutes(routes)}
</StaticRouter>
</Router>
</Provider>
</Loadable.Capture>
);
const content = ReactDOM.renderToString(component);
if (context.url) {
return res.redirect(301, context.url);
}
const { location } = history;
if (decodeURIComponent(req.originalUrl) !== decodeURIComponent(location.pathname + location.search)) {
return res.redirect(301, location.pathname);
}
const bundles = getBundles(getChunks(), modules);
const html = <Html assets={webpackIsomorphicTools.assets()} bundles={bundles} content={content} store={store} />;
res.status(200).send(`<!doctype html>${ReactDOM.renderToString(html)}`);
} catch (mountError) {
console.error('MOUNT ERROR:', pretty.render(mountError));
res.status(500);
hydrate();
}
});
(async () => {
if (config.port) {
try {
await Loadable.preloadAll();
await waitChunks(chunksPath);
} catch (error) {
console.log('Server preload error:', error);
}
server.listen(config.port, err => {
if (err) {
console.error(err);
}
console.info('----\n==> ✅ %s is running, talking to API server on %s.', config.app.title, config.apiPort);
console.info('==> 💻 Open http://%s:%s in a browser to view the app.', config.host, config.port);
});
} else {
console.error('==> ERROR: No PORT environment variable has been specified');
}
})();