Skip to content

Instantly share code, notes, and snippets.

@remy
Created September 17, 2014 20:46
Show Gist options
  • Save remy/50f1758a74242d51a90f to your computer and use it in GitHub Desktop.
Save remy/50f1758a74242d51a90f to your computer and use it in GitHub Desktop.
Sample node server that sends chunked script tags.
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, { 'content-type': 'text/html' });
res.write('<script>console.log("this is the start of the stream...")</script>');
var timer = setInterval(function () {
if (!res.connection || !res.connection.writable) {
try { res.end(); } catch (e) {}
clearInterval(timer);
console.log('closed');
return;
}
res.write('<script>console.log("and now more messages...")</script>');
}, 2000);
// importantly, res.end() is never called.
});
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment