-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (63 loc) · 1.9 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
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import express from 'express';
import path from 'path';
import http from 'http';
import bodyParser from 'body-parser';
import webpackConfig from './webpack.config';
const isProduction = process.env.NODE_ENV === 'production';
const isDeveloping = !isProduction;
const app = express();
// Webpack dev server
if (isDeveloping) {
const WEBPACK_PORT = 3000;
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
noInfo: false,
quiet: false,
hot: true,
stats: {
colors: true,
hash: false,
version: false,
timings: false,
assets: false,
chunks: false,
modules: false,
reasons: false,
children: false,
source: false,
errors: false,
errorDetails: false,
warnings: false,
publicPath: 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(bodyParser.json({ type: 'application/json' }))
app.use(express.static(publicPath));
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);
});