forked from erikras/react-redux-universal-hot-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_es6.js
60 lines (48 loc) · 1.58 KB
/
start_es6.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
#!/usr/bin/env node
import path from 'path';
import http from 'http';
import renderer from 'universal-redux';
import httpProxy from 'http-proxy';
import config from '../config/universal-redux.config.js';
import SocketIo from 'socket.io';
const isProduction = process.env.NODE_ENV !== 'production';
const apiPort = process.env.APIPORT;
const apiHost = process.env.APIHOST || 'localhost';
const apiEndpoint = '/api';
function setupProxy(app) {
const proxy = httpProxy.createProxyServer({
target: 'http://' + apiHost + ':' + apiPort,
ws: true
});
// Proxy to API server
app.use(`${apiEndpoint}`, (req, res) => {
proxy.web(req, res);
});
// added the error handling to avoid https://github.com/nodejitsu/node-http-proxy/issues/527
proxy.on('error', (error, req, res) => {
let json;
if (error.code !== 'ECONNRESET') {
console.error('proxy error', error);
}
if (!res.headersSent) {
res.writeHead(500, {'content-type': 'application/json'});
}
json = {error: 'proxy_error', reason: error.message};
res.end(JSON.stringify(json));
});
}
const app = renderer.app();
setupProxy(app);
renderer.setup(config);
const server = new http.Server(app);
if (!isProduction) {
const io = new SocketIo(server);
io.path(`${__API_ENDPOINT__}/ws`);
}
server.listen(config.server.port, (err) => {
if (err) {
console.error(err);
}
console.info('==> 🌎 API calls will be received at:', config.server.host + ':' + config.server.port + apiEndpoint);
console.info('==> 💻 Open http://localhost:%s in a browser to view the app.', config.server.port);
});