Created
December 8, 2013 12:51
-
-
Save anttti/7857006 to your computer and use it in GitHub Desktop.
Node serving a chunked response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
getNow = function() { return JSON.stringify({ time: (new Date()).getTime().toString() }) + '\n' }; | |
http.createServer(function (request, response) { | |
response.setHeader('Content-Type', 'application/json; charset=UTF-8'); | |
response.setHeader('Transfer-Encoding', 'chunked'); | |
console.log('start'); | |
response.write(getNow()); | |
var interval = setInterval(function() { | |
var msg = getNow(); | |
console.log(msg); | |
response.write(msg); | |
}, 1000); | |
setTimeout(function() { | |
clearInterval(interval); | |
var msg = getNow(); | |
console.log(msg); | |
response.end(msg); | |
}, 5000); | |
}).listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment