-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (52 loc) · 1.82 KB
/
app.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
var express = require('express'),
path = require('path'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
consolidate = require('consolidate'),
routes = require('./routes/index'),
config = require('./config');
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const wpConfig = require('./webpack.config.js');
const webpackHotMiddleware = require('webpack-hot-middleware');
var app = express();
app.engine('dust', consolidate.dust);
app.set('view engine', 'dust');
app.set('views', './src');
app.use(bodyParser.json());
app.use('/server', routes);
app.set('port', process.env.PORT || 9090);
const isDeveloping = process.env.NODE_ENV !== 'production';
console.log("in dev mode", isDeveloping)
console.log("in prod mode", !isDeveloping)
const port = isDeveloping ? 9090 : process.env.PORT;
if (isDeveloping) {
const compiler = webpack(wpConfig);
const middleware = webpackMiddleware(compiler, {
publicPath: wpConfig.output.publicPath,
contentBase: 'src',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
} else {
app.use(express.static(__dirname + '/dist'));
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
module.exports = app;