Home > Web Front-end > JS Tutorial > Sample code for using HTTP chunked response and timer in nodejs

Sample code for using HTTP chunked response and timer in nodejs

巴扎黑
Release: 2017-05-14 14:29:27
Original
1249 people have browsed it

This article uses an example to create an HTTP server that outputs plain text. The output plain text will add 100 timestamps separated by newlines every second. The example code is very good and has reference value. Friends who need it can refer to it

In this example, we will create an HTTP server that outputs plain text. The output plain text will add 100 new users every second. Newline separated timestamps.


require('http').createServer(function(req, res) {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 var left = 10;
 var interval = setInterval(function() {
 for(var i = 0; i< 100; i++) {
  res.write(Date.now() + " ");
 }
 if(-- left === 0) {
  clearInterval(interval);
  res.end();
 }
 }, 1000);
}).listen(4000);;
Copy after login

The above is the detailed content of Sample code for using HTTP chunked response and timer in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template