-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
70 lines (58 loc) · 2.01 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
// this is run by heroku and serves the built artifacts in the dist directory
var express = require('express');
var path = require('path');
var httpProxy = require('http-proxy');
var http = require('http');
var proxy = httpProxy.createProxyServer({
changeOrigin: true,
ws: true
});
var app = express();
var isProduction = process.env.NODE_ENV === 'production';
var port = isProduction ? process.env.PORT : 3002;
var publicPath = isProduction ? path.resolve(__dirname, 'dist') : path.resolve(__dirname, 'build');
app.use(express.static(publicPath));
if(isProduction){
// Production means heroku or a plain server .. not a http server ..
// here we're just starting a basic express server to serve the
// built assets of the project, which will end up in dist.
app.listen(port, function () {
console.log('Server running on port ' + port);
});
} else {
// we're running on the local webpack dev server
// although we'll start an express server and give you the option ...
// to create your own rest api or even socket.io
var devServer = require('./devServer.js');
devServer();
// express server
// define your rest api
app.get('/api/post', function (req, res) {
res.send([{_id:1, body:'post test'}]);
});
// serve content from the webpack dev server on /build/*
// the reason for this is so that we can differ the express server from the dev server.
app.all('/build/*', function (req, res) {
proxy.web(req, res, {
target: 'http://127.0.0.1:3001'
});
});
//app.all('/socket.io*', function (req, res) {
// proxy.web(req, res, {
// target: 'http://127.0.0.1:3001'
// });
//});
proxy.on('error', function(e) {
// Just catch it
console.error(e);
});
// We need to use basic HTTP service to proxy
// websocket requests from webpack
var server = http.createServer(app);
server.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head);
});
server.listen(port, function () {
console.log('Server running on port ' + port);
});
}