-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (56 loc) · 1.95 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
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import proxyMiddleware from 'http-proxy-middleware';
import express from 'express';
import path from 'path';
import http from 'http';
import bodyParser from 'body-parser';
import webpackConfig from './webpack.config';
import { SERVER_URL } from './src/config';
const isProduction = process.env.NODE_ENV === 'production';
const isDeveloping = !isProduction;
const app = express();
// Webpack dev server
if (isDeveloping) {
const WEBPACK_PORT = 3001;
const compiler = webpack(webpackConfig);
app.use(webpackMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
}));
app.use(webpackHotMiddleware(compiler));
app.listen(WEBPACK_PORT, 'localhost', function (err, result) {
if (err) {
console.log(err);
}
console.log('WebpackDevServer listening at localhost:'+WEBPACK_PORT);
});
}
// RESTful API
const publicPath = path.resolve(__dirname);
// app.use(proxyMiddleware('/api', { target: SERVER_URL, pathRewrite: { '^/api': '' }, changeOrigin: true }));
// app.use(['/api', '/oauth', '/assets'], proxyMiddleware({target: SERVER_URL, changeOrigin: true}));
app.use(bodyParser.json({ type: 'application/json' }))
app.use(express.static('static'));
const port = isProduction ? (process.env.PORT || 80) : 3000;
// this is necessary to handle URL correctly since client uses Browser History
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, '', 'index.html'))
})
// We need to use basic HTTP service to proxy
// websocket requests from webpack
const server = http.createServer(app);
server.listen(port, function (err, result) {
if(err){
console.log(err);
}
console.log('Server running on port ' + port);
});