-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (43 loc) · 1.57 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
var express = require('express');
var path = require('path');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
var app = express();
var isProduction = process.env.NODE_ENV === 'production';
var port = isProduction ? process.env.PORT : 3000;
var publicPath = path.resolve(__dirname, 'public');
app.use(express.static(publicPath));
// We only want to run the workflow when not in production
if (!isProduction) {
// We require the bundler inside the if block because
// it is only needed in a development environment. Later
// you will see why this is a good idea
console.log("in !production block");
var bundle = require('./server/bundle.js');
bundle();
// Any requests to localhost:3000/build is proxied
// to webpack-dev-server
app.all('/build/*', function (req, res) {
console.log('a request that will be proxied');
//console.log(req);
proxy.web(req, res, {
target: 'http://localhost:8080'
});
});
}
app.get('*', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
// It is important to catch any errors from the proxy or the
// server will crash. An example of this is connecting to the
// server when webpack is bundling
proxy.on('error', function(e) {
console.log('Could not connect to proxy, please try again...');
});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
console.log("proxy request coming in");
//proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
app.listen(port, function () {
console.log('Server.js server running on port ' + port);
});