var http = require("http");
function generateChunk(index, response) {
setTimeout(() => {
if (index === 5) {
response.write("end");
response.end("</body></html>");
} else {
response.write(</span><p> chunk <span class="pl-s1"><span class="pl-pse">${</span>index<span class="pl-pse">}</span></span></p><span class="pl-pds">
);
}
}, index * 1000);
}
function handlerRequest(_request, response) {
response.setHeader("Content-Type", "text/html; charset=UTF-8");
response.setHeader("Transfer-Encoding", "chunked");
response.write(</span><!DOCTYPE html></span> <span class="pl-s"> <html lang="en"></span> <span class="pl-s"> <head></span> <span class="pl-s"> <meta charset="utf-8"></span> <span class="pl-s"> <title>HTTP 分块传输示例</title></span> <span class="pl-s"> </head></span> <span class="pl-s"> <body></span> <span class="pl-s"> <h1>HTTP 分块传输示例</h1></span> <span class="pl-s"> <span class="pl-pds">
);
let index = 0;
while (index <= 5) {
generateChunk(index, response);
index++;
}
}
const server = http.createServer(handlerRequest);
server.listen(3000);
console.log("server started at http://localhost:3000");