-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·71 lines (54 loc) · 1.93 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
'use strict';
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpack from 'webpack';
import config from './webpack.config.js';
const DEV_MODE = process.env.NODE_ENV !== 'production';
const ROOT_DIR = path.resolve( __dirname, '.' ); // .. when we get into it
const resolvePath = ( ...args ) => path.resolve( ROOT_DIR, ...args );
const SRC_DIR = resolvePath( 'src' );
const BUILD_DIR = resolvePath( 'dist' );
const compiler = webpack(config);
const options = DEV_MODE ? {
port: process.env.BIND_PORT || 3000,
host: process.env.BIND_HOST || '0.0.0.0',
} : {
port: process.env.BIND_PORT || 80,
host: process.env.BIND_HOST || '0.0.0.0',
};
express.static.mime.define({'application/json': ['json']});
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
let compilerOpts = compiler.options;
if (compiler.compilers) {
let d = compiler.compilers.filter( el => { return el.options.name == 'client'; })[0];
compilerOpts = d.options;
}
if (DEV_MODE) {
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: compilerOpts.output.publicPath || '/',
watchOptions: { poll: true },
serverSideRender: true,
index: true,
hot: true, // should this be here?
}));
app.use(webpackHotMiddleware(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 2000,
}));
} else {
app.use(compilerOpts.output.publicPath || '/', express.static('dist'));
}
import Api from './src/Api';
app.use(Api);
app.listen(options.port, options.host, (err) => {
if (err) {
throw err;
}
console.log(( DEV_MODE ? '[DEV] ' : '' ) + `Listening on ${options.host}:${options.port}`);
});