-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (48 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
57
require('babel-core/register');
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const dev = require('webpack-dev-middleware');
const hot = require('webpack-hot-middleware');
const config = require('./webpack.config.js');
const port = process.env.PORT || 3000;
const server = express();
global.__ENVIRONMENT__ = process.env.NODE_ENV || 'default';
// Otherwise errors thrown in Promise routines will be silently swallowed.
// (e.g. any error during rendering the app server-side!)
process.on('unhandledRejection', (reason, p) => {
if (reason.stack) {
console.error(reason.stack);
} else {
console.error('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
}
});
// Short-circuit the browser's annoying favicon request. You can still
// specify one as long as it doesn't have this exact name and path.
server.get('/favicon.ico', function(req, res) {
res.writeHead(200, { 'Content-Type': 'image/x-icon' });
res.end();
});
server.use(express.static(path.resolve(__dirname, 'dist')));
if (!process.env.NODE_ENV) {
const compiler = webpack(config);
server.use(dev(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
}));
server.use(hot(compiler));
} else {
server.use(require('compression')());
}
server.get('*', require('./app').default);
server.listen(port, (err) => {
if (err) console.error(err);
console.info(`Server running on http://localhost:${port}/`);
});