1

I'm trying to send responses in chunks with gzip but from following example I'm getting "Hello�S�/�I�m���S��k�" in Chrome instead "Hello World!"

var http = require('http'), zlib = require('zlib'); http.createServer(function(req, res) { res.writeHead(200, { 'Content-Type': 'html/text', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=UTF-8' }); zlib.gzip("Hello", function(_, result) { res.write(result); }); zlib.gzip(" World", function(_, result) { res.write(result); }); zlib.gzip('!', function(_, result) { res.end(result); }); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 

1 Answer 1

2

That is not what is meant by chunked transfer encoding.

See the description in the HTTP standard. In short chunked encoding consists of chunks with an ASCII hex chunk length followed by a CRLF, then that many bytes, then another CRLF. Repeat, end with a zero length chunk and another CRLF for good measure after the zero-length chunk CRLF.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but not sure how that's related with problem I described. could you please provide more details ?
You are lying to the HTTP client by saying that there will be chunked encoding following, but then not doing that. If you are not constructing the HTTP stream per the standard, you cannot expect a good result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.