2013-07-29 25 views
1

我試圖用gzip發送響應塊,但從下面的例子中我在Chrome中得到「Hello S/I m S k 」而是「Hello World!」node.js streaming gzip http responses

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

回答

2

這不是分塊傳輸編碼的含義。

查看description in the HTTP standard。簡而言之,分塊編碼由具有ASCII十六進制塊長度的塊,隨後是CRLF,然後是多個字節,然後是另一個CRLF組成。重複,以零長度塊和另一個CRLF結束,以在零長度塊CRLF之後進行較好的測量。

+0

謝謝,但不知道如何與我描述的問題有關。你能否提供更多細節? –

+0

你對HTTP客戶端說謊,說下面會有分塊編碼,但是不會那麼做。如果你不是按照標準構建HTTP流的話,你不能指望一個好的結果。 –