Sample code for using HTTP block response and timer in nodejs

  • 2021-08-05 08:13:36
  • OfStack

In this example, you are going to create an HTTP server that outputs plain text with 100 newline-delimited timestamps every 1 second.


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);;

The above is the site to introduce the use of nodejs HTTP block response and timer example code, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: