-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (57 loc) · 1.81 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
import express from 'express';
import path from 'path';
import cors from 'cors';
const PORT = 7705;
const PUBLIC_PATH = __dirname + '/dist';
const app = express();
app.use(cors());
const isDevelopment =
process.env.NODE_ENV && process.env.NODE_ENV.trim() !== 'production';
console.log('WEBPACK_MODE :' + process.env.NODE_ENV);
console.log(typeof process.env.NODE_ENV);
console.log('isDevelopment :' + isDevelopment);
if (isDevelopment) {
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.dev.babel');
webpackConfig.mode = 'development';
const compiler = webpack(webpackConfig);
console.log(webpackConfig.output);
console.log(webpackConfig.entry);
app.use(
require('webpack-dev-middleware')(compiler, {
contentBase: './dist',
host: 'loclahost',
hot: true,
stats: {
colors: true
},
publicPath: webpackConfig.output.publicPath
})
);
app.use(
require('webpack-hot-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
//path: '/__webpack_hmr',
})
);
app.use('*', (req, res, next) => {
const filename = path.resolve(compiler.outputPath, 'index.html');
compiler.outputFileSystem.readFile(filename, (err, result) => {
if (err) {
return next(err);
}
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
} else {
app.use(express.static(PUBLIC_PATH));
}
//app.use(express.static(PUBLIC_PATH));
app.all('*', function(req, res) {
res.sendFile(path.resolve(PUBLIC_PATH, 'index.html'));
});
app.listen(PORT, function() {
console.log('Listening on port ' + PORT + '...');
});