简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM