如果要进行分块传输编码,则实际上需要设置该标头:
Transfer-Encoding: chunked
您可以从google返回的标头中看到该标头,该标头会对主页以及最可能的其他页面进行分块传输:
HTTP/1.1 200 OKDate: Sat, 04 Jun 2011 00:04:08 GMTExpires: -1Cache-Control: private, max-age=0Content-Type: text/html; charset=ISO-8859-1Set-cookie: PREF=ID=f9c65f4927515ce7:FF=0:TM=1307145848:LM=1307145848:S=fB58RFtpI5YeXdU9; expires=Mon, 03-Jun-2013 00:04:08 GMT; path=/; domain=.google.comSet-cookie: NID=47=UiPfl5ew2vCEte9JyBRkrFk4EhRQqy4dRuzG5Y-xeE---Q8AVvPDQq46GYbCy9VnOA8n7vxR8ETEAxKCh-b58r7elfURfiskmrOCgU706msiUx8L9qBpw-3OTPsY-6tl; expires=Sun, 04-Dec-2011 00:04:08 GMT; path=/; domain=.google.com; HttpOnlyServer: gwsX-XSS-Protection: 1; mode=blockTransfer-Encoding: chunked
编辑 Yikes,读起来太复杂了:
var app = function(req,res){ var head={'Content-Type':'text/html'} switch(req.url.slice(-3)){ case '.js':head={'Content-Type':'text/javascript'};break; case 'css':head={'Content-Type':'text/css'};break; case 'png':head={'Content-Type':'image/png'};break; case 'ico':head={'Content-Type':'image/x-icon'};break; case 'ogg':head={'Content-Type':'audio/ogg'};break; case 'ebm':head={'Content-Type':'video/webm'};break; } res.writeHead(200,head) var file_stream = fs.createReadStream('.'+req.url); file_stream.on("error", function(exception) { console.error("Error reading file: ", exception); }); file_stream.on("data", function(data) { res.write(data); }); file_stream.on("close", function() { res.end(); });}到了那里,一个不错的流缓冲供您编写。 这是我写的一篇博客文章,介绍了几种读取文件的方法。
我建议您仔细研究一下,以便您可以了解如何在节点的异步环境中更好地处理文件。



