-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.js
More file actions
40 lines (35 loc) · 1 KB
/
streaming.js
File metadata and controls
40 lines (35 loc) · 1 KB
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
const http = require("http");
const PORT = 3000;
const HOST = "localhost";
const base = `http://${HOST}:${PORT}`;
const server = http.createServer((req, res) => {
const url = new URL(req.url, base);
res.write(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>`);
let count = 0;
let prevClassName = null;
const id = setInterval(() => {
count += 0.1;
const className = `count-${Math.floor(Math.random() * 1e6)}`;
res.write(`<!-- start section -->`);
if (prevClassName) {
res.write(`<style>.${prevClassName} { display: none; }</style>`);
}
res.write(`<h1 class="${className}">${count.toFixed(1)}</h1>`);
res.write(`<!-- end section -->`);
prevClassName = className;
}, 100).unref();
req.addListener("close", () => {
clearInterval(id);
res.end("");
});
});
server.listen(PORT, HOST, () => {
console.log(`Listening on ${base}`);
});