-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (48 loc) · 1.81 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
import path from 'path';
import express from 'express';
import webpackDevServer from './webpack.dev.server';
import bodyParser from 'body-parser'
import compression from 'compression'
export default function(callback) {
const server = express();
server.disable('x-powered-by') // Disable useless headers
server.set('env', process.env.NODE_ENV)
server.set('host', process.env.HOST || 'localhost')
server.set('port', process.env.PORT || 3000)
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({extended: false}))
if (process.env.NODE_ENV === 'production') {
server.use(compression())
}
global.__ENVIRONMENT__ = process.env.NODE_ENV || 'development';
// 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')));
server.use(express.static(path.resolve(__dirname, 'api')));
server.use(express.static(path.resolve(__dirname, 'assets')));
// Pull in development server before we define our routes
if (process.env.NODE_ENV === 'development') {
webpackDevServer(server)
}
server.get('*', require('./app').serverMiddleware);
server.listen(server.get('port'), (err) => {
if (err) {
console.error(err);
}
callback(server)
});
return server;
}