-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevServer.js
55 lines (44 loc) · 1.4 KB
/
devServer.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
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const config = require('./webpack.config');
const app = express();
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
}));
app.use(require('webpack-hot-middleware')(compiler));
app.use('/files', express.static(path.join(__dirname, 'files')));
// app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
app.use('/:lang', (req, res, next) => {
const lang = req.params.lang ? req.params.lang : 'en';
const filename = path.join(compiler.outputPath, `index_${lang}.html`);
compiler.outputFileSystem.readFile(filename, (err, html) => {
if (err) {
return next(err);
}
res.set('content-type', 'text/html');
res.send(html);
return res.end();
});
});
app.use('/', (req, res, next) => {
const lang = 'en';
const filename = path.join(compiler.outputPath, `index_${lang}.html`);
compiler.outputFileSystem.readFile(filename, (err, html) => {
if (err) {
return next(err);
}
res.set('content-type', 'text/html');
res.send(html);
return res.end();
});
});
app.listen(3000, 'localhost', (err) => {
if (err) {
console.log(err); // eslint-disable-line
return;
}
console.log('Listening at http://localhost:3000'); // eslint-disable-line
});