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