-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (47 loc) · 1.53 KB
/
Copy pathserver.js
File metadata and controls
57 lines (47 loc) · 1.53 KB
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
const path = require('path');
const webpack = require('webpack');
const express = require('express');
if (process.env['NODE_ENV'] !== 'production') {
var devMiddleware = require('webpack-dev-middleware');
var hotMiddleware = require('webpack-hot-middleware');
}
const app = express();
// Initialize the production server which serves up webpack'ed files.
const initProductionServer = () => {
const distDir = __dirname + '/dist';
app.use('/static', express.static(distDir));
};
// Initialize the development server which hot loads source content webpack.
const initDevelopmentServer = function () {
const config = require('./webpack.config.dev');
const compiler = webpack(config);
app.use(devMiddleware(compiler, {
publicPath: config.output.publicPath,
historyApiFallback: true,
stats: {
colors: true
}
}));
app.use(hotMiddleware(compiler));
// app.use('/static', express.static(__dirname + '/src/static'));
};
// Depending on mode, initialize the server.
if (process.env['NODE_ENV'] === 'production') {
initProductionServer();
} else {
initDevelopmentServer();
}
// In both dev and prod, serve up the top level 'index.html' file.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'src/index.html'));
});
// Set up the application port (or from environment if hosted).
const appHttpPort = process.env.PORT || 3000;
// Start web server.
app.listen(appHttpPort, '0.0.0.0', (err) => {
if (err) {
console.log(err);
} else {
console.log('Url: http://localhost:' + appHttpPort);
}
});