-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (64 loc) · 1.86 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
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 falcorExpress = require('falcor-express');
var Router = require('falcor-router');
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));
app.use('/model.json', falcorExpress.dataSourceRoute(function (req, res) {
// create a Virtual JSON resource with single key ("greeting")
return new Router([
{
// match a request for the key "greeting"
route: "greeting",
// respond with a PathValue with the value of "Hello World."
get: function() {
return {path:["greeting"], value: "Hello World"};
}
}
]);
}));
app.all('/db/*', function (req, res) {
proxy.web(req, res, {
target: 'https://glowing-carpet-4534.firebaseio.com/'
});
});
if (!isProduction) {
var bundle = require('./server/bundle.js');
bundle();
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
});
// 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);
});
} else {
// And run the server
app.listen(port, function () {
console.log('Server running on port ' + port);
});
}