Skip to content

Instantly share code, notes, and snippets.

@kadishmal
Created October 11, 2012 08:36
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kadishmal/3871017 to your computer and use it in GitHub Desktop.
Save kadishmal/3871017 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);
@kadishmal
Copy link
Author

The gist illustrates the basic functionality of an http server which sends data to a client in chunks using chunked transfer encoding. setTimeout function was used to imitate the time consuming requests.

@jalleyne
Copy link

Hey @kadishmal, i was having some issues getting chunked responses to work and came across your gist. I used it to see if i would get similar results with my own script and they both seem to buffer the response until end is called. Do you know of any changes in node.js that would affect this? have you tested it recently? thanks

@felixrabe
Copy link

@jalleyne, you might want to test using curl. Even without a Transfer-Encoding header, it will show the individual chunks as they come in.

@jerryyanmj
Copy link

One thing which is confusing is the way you ended your html code. How do you know that it is your last chunk? In real time example, if you have a number of async calls to fetch information, you cannot tell which one comes back first. Can suggest a workable solution of closing the html based on the last async call?

@amn
Copy link

amn commented May 23, 2017

Is this really chunked transfer encoding implemented properly? Where do you actually encode the data you are writing on the response? Are you assuming that Node.js does the chunked encoding for you down the line? With chunked transfer encoding, each chunk should be preceded by its size in hexadecimal format, and the last chunk is a zero-size chunk. I don't see any encoding in your gist, so I am assuming that you are expecting Node.js to do the actual encoding for you? Without encoding this is just a response that is terminated with a connection close event by server (which is valid HTTP/1.1 but negates the value of keep-alive feature).

@m44-io
Copy link

m44-io commented May 30, 2020

Is this really chunked transfer encoding implemented properly? Where do you actually encode the data you are writing on the response? Are you assuming that Node.js does the chunked encoding for you down the line? With chunked transfer encoding, each chunk should be preceded by its size in hexadecimal format, and the last chunk is a zero-size chunk. I don't see any encoding in your gist, so I am assuming that you are expecting Node.js to do the actual encoding for you? Without encoding this is just a response that is terminated with a connection close event by server (which is valid HTTP/1.1 but negates the value of keep-alive feature).

@amn

Node.js IS encoding the chunked transfer, thats the whole point of the internal http module. If you set the header to Transfer-Encoding: chunked then the http outgoing handler checks for it and applies the proper hexadecimal length and new line terminators for each write.

I even went to go find you exactly where it's doing it in the Node.ja offficial repository:
https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L642-L728

@amn
Copy link

amn commented Sep 8, 2021

@m44-io, got it. It's been a while I wrote my inquiry, but thank you for your explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment