-
Notifications
You must be signed in to change notification settings - Fork 0
/
devserver.js
60 lines (49 loc) · 1.21 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
56
57
58
59
60
process.env.UV_THREADPOOL_SIZE = 100;
const express = require('express');
const faker = require('faker');
const webpack = require('webpack');
const config = require('./webpack.config');
const port = 8000;
const app = express();
const compiler = webpack(config);
app.use(express.static(process.cwd()));
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
}));
app.use(require('webpack-hot-middleware')(compiler));
app.get('/error', (req, res) => {
res.status(500).send({
error: 'bang',
});
});
app.get('/stream', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/html',
'Transfer-Encoding': 'chunked',
'Cache-Control': 'no-cache',
});
function chunk(i) {
const text = faker.lorem.paragraph();
const html = `<html><h1>chunk #${i}</h1><p>${text}</p></html>`;
const data = `00${html.length.toString(16)};name=value\r\n${html}\r\n`;
res.write(data);
if (i < 100) {
setTimeout(() => {
chunk(i + 1);
}, 100);
} else {
res.write('0\r\n\r\n');
}
}
setTimeout(() => {
chunk(1);
}, 100);
});
app.listen(port, '0.0.0.0', (err) => {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://0.0.0.0:%s', port);
});