-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
39 lines (31 loc) · 1.13 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
var express = require('express');
var path = require('path');
var httpProxy = require('http-proxy');
var publicPath = path.resolve(__dirname, 'public');
var port = 3000;
// We need to add a configuration to our proxy server,
// as we are now proxying outside localhost
var proxy = httpProxy.createProxyServer({
changeOrigin: true
});
var app = express();
//serving our index.html
app.use(express.static(publicPath));
//server/compiler.js runs webpack-dev-server which creates the bundle.js which index.html serves
//the compiler adds some console logs for some extra sugar
//notice that you will not see a physical bundle.js because webpack-dev-server runs it from memory
var bundle = require('./server/compiler.js')
bundle()
//express now processes all requests to localhost:8080
//app.all is a special routing method used for loading middleware functions
app.all('/build/*', function (req, res) {
proxy.web(req, res, {
target: 'http://localhost:8080'
})
})
proxy.on('error', function(e) {
console.log('Could not connect to proxy, please try again...')
});
app.listen(port, function () {
console.log('Server running on port ' + port)
});