-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
41 lines (37 loc) · 1.33 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
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const express = require('express');
const compression = require('compression');
const historyApiFallback = require('connect-history-api-fallback');
const jsonServer = require('json-server');
const config = require('./config/application');
const webpackDevConfig = require('./config/webpack_dev.config');
const webpackBuildConfig = require('./config/webpack_build.config');
const port = config.port;
const server = express();
if (config.development) {
const compiler = webpack(webpackDevConfig);
server.use(historyApiFallback());
server.use(webpackDevMiddleware(compiler, {
stats: {
chunks: false,
colors: true
},
publicPath: webpackDevConfig.output.publicPath
}));
server.use(webpackHotMiddleware(compiler));
server.use(jsonServer.defaults());
server.use(config.apiPath, jsonServer.router('./db/db.json'));
server.listen(port, 'localhost', () => {
console.log(`Server listening on port ${port}`);
});
} else {
webpack(webpackBuildConfig, (err, stats) => {
if (err) return console.log(err);
server.use(compression());
server.use(historyApiFallback());
server.use(express.static(config.distDir));
server.listen(port);
});
}