Skip to content

Instantly share code, notes, and snippets.

@mdsaleemj
Forked from kadishmal/app.js
Created March 12, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsaleemj/a216cc494f9a8cb8d7c6109572c1c4d8 to your computer and use it in GitHub Desktop.
Save mdsaleemj/a216cc494f9a8cb8d7c6109572c1c4d8 to your computer and use it in GitHub Desktop.
Simple Node.js server which responds in chunked transfer encoding
var http = require('http');
http.createServer(function (request, response) {
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
response.setHeader('Transfer-Encoding', 'chunked');
var html =
'<!DOCTYPE html>' +
'<html lang="en">' +
'<head>' +
'<meta charset="utf-8">' +
'<title>Chunked transfer encoding test</title>' +
'</head>' +
'<body>';
response.write(html);
html = '<h1>Chunked transfer encoding test</h1>'
response.write(html);
// Now imitate a long request which lasts 5 seconds.
setTimeout(function(){
html = '<h5>This is a chunked response after 5 seconds. The server should not close the stream before all chunks are sent to a client.</h5>'
response.write(html);
// since this is the last chunk, close the stream.
html =
'</body>' +
'</html';
response.end(html);
}, 5000);
// this is another chunk of data sent to a client after 2 seconds before the
// 5-second chunk is sent.
setTimeout(function(){
html = '<h5>This is a chunked response after 2 seconds. Should be displayed before 5-second chunk arrives.</h5>'
response.write(html);
}, 2000);
}).listen(process.env.VMC_APP_PORT || 1337, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment