-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (48 loc) · 1.61 KB
/
index.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
66
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const webpack = require('webpack');
const app = express();
const cors = require('cors');
app.use(cors());
app.use(bodyParser.json());
if (process.env.NODE_ENV === 'development') {
const webpackMiddleware = require('webpack-dev-middleware');
const webpackDevMiddleware = require('webpack-hot-middleware');
let config = require('./webpack.config.dev');
let compiler = webpack(config);
let DIR_DIST = path.resolve(__dirname, 'frontend', 'dist');
app.use((req, res, next) => {
console.log(req.body);
next();
});
app.use(webpackMiddleware(compiler, {
publicPath: config.output.publicPath
}));
app.use(webpackDevMiddleware(compiler));
app.get('*', (req, res, next) => {
const filename = path.join(DIR_DIST, '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 if (process.env.NODE_ENV == 'production') {
app.use('/static', express.static(path.join(__dirname, './frontend/dist')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './frontend/dist/index.html'))
});
app.get('*', (req, res) => {
res.redirect('/');
})
} else {
throw new Error('Invalid Value for NODE_ENV');
}
app.listen(8080, () => {
console.log("Express app listening on port " + 80);
});