-
Notifications
You must be signed in to change notification settings - Fork 88
/
app.js
62 lines (47 loc) · 1.91 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
var express = require('express'),
path = require('path'),
consolidate = require('consolidate');
var isDev = process.env.NODE_ENV !== 'production';
var app = express();
var port = 3000;
app.engine('html', consolidate.ejs);
app.set('view engine', 'html');
app.set('views', path.resolve(__dirname, './server/views'));
// local variables for all views
app.locals.env = process.env.NODE_ENV || 'dev';
app.locals.reload = true;
if (isDev) {
// static assets served by webpack-dev-middleware & webpack-hot-middleware for development
var webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
webpackDevConfig = require('./webpack.config.js');
var compiler = webpack(webpackDevConfig);
// attach to the compiler & the server
app.use(webpackDevMiddleware(compiler, {
// public path should be the same with webpack config
publicPath: webpackDevConfig.output.publicPath,
stats: 'errors-only'
}));
app.use(webpackHotMiddleware(compiler));
require('./server/routes')(app);
// add "reload" to express, see: https://www.npmjs.com/package/reload
var reload = require('reload');
var http = require('http');
var server = http.createServer(app);
reload(app).then(function (reloadReturned) {
// start web server after reload started
server.listen(port, function(){
console.log('App (dev) is now running on port 3000!');
});
}).catch(function (err) {
console.error('Reload could not start, could not start server', err);
});
} else {
// static assets served by express.static() for production
app.use(express.static(path.join(__dirname, 'public')));
require('./server/routes')(app);
app.listen(port, function () {
console.log('App (production) is now running on port 3000!');
});
}