forked from jcgertig/zer0tv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (46 loc) · 1.7 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
const path = require('path');
const emojic = require('emojic');
const colorIt = require('color-it');
const express = require('express');
const compress = require('compression');
const app = express();
const PORT = process.env.NODE_ENV !== 'production' ? 4000 : process.env.PORT || 4000;
const green = txt => colorIt(txt).green();
const red = txt => colorIt(txt).red();
const blue = txt => colorIt(txt).blue();
app.use(compress());
app.use('/public', express.static('public'));
app.use('/dist', express.static('dist'));
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack'); // eslint-disable-line
const config = require('./config/webpack.config.js'); // eslint-disable-line
const compiler = webpack(config);
// eslint-disable-next-line
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
}));
// eslint-disable-next-line
app.use(require('webpack-hot-middleware')(compiler));
app.get('*', (req, res, next) => {
const filename = path.join(compiler.outputPath, 'index.html');
compiler.outputFileSystem.readFile(filename, (err, result) => {
if (err) { return next(err); }
res.set('content-type', 'text/html');
res.send(result);
return res.end();
});
});
} else {
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
}
app.listen(PORT, (err) => {
if (err) {
console.log(`${red(emojic.x)} ${red(JSON.stringify(err))}`); // eslint-disable-line no-console
return;
}
const msg = `${green(emojic.smiley)}${green(' Listening at http://localhost:')}${blue(PORT)}${green('.')}`;
console.log(msg); // eslint-disable-line no-console
});