-
Notifications
You must be signed in to change notification settings - Fork 9
/
http.js
43 lines (34 loc) · 880 Bytes
/
http.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
var http = require('http');
var app = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html', 'charset': 'utf-8' });
res.write('<br>loading...');
timer(5, res);
});
var isEnd = false;
/** 生成倒计时渲染 */
function timer(num, res) {
isEnd = false;
var t = Math.floor(Math.random() * 10) * 3000;
setTimeout(function () {
if (isEnd) {
return;
}
if (num == 1) {
isEnd = true;
res.end(`<div>last timer: ${t}ms</div>`);
} else {
res.write(`<div>timer${num} : ${t}ms</div>`);
}
}, t);
if (num > 1) {
timer(num - 1, res);
}
}
/** 异常处理 */
process.on('uncaughtException', function (err) {
console.log(err);
});
module.exports = function () {
app.listen(9090);
console.log('server on 9090');
};