Ivan Lazarevic
Ivan Lazarevic

Reputation: 371

node.js streaming gzip http responses

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/');

Upvotes: 1

Views: 551

Answers (1)

Mark Adler
Mark Adler

Reputation: 112239

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.

Upvotes: 2

Related Questions