Node.js:分块传输编码

时间:2011-06-03 23:20:47

标签: node.js

该代码是否有效HTTP / 1.1?

var fs = require('fs')
var http = require('http')

var buf=function(res,fd,i,s,buffer){
 if(i+buffer.length<s){
  fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){
   res.write(b.slice(0,l))
   //console.log(b.toString('utf8',0,l))
   i=i+buffer.length
   buf(res,fd,i,s,buffer)
  })
 }
 else{
  fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){
   res.end(b.slice(0,l))
   fs.close(fd)
  })
 }
}

var app = function(req,res){
 var head={'Content-Type':'text/html; charset=UTF-8'}
 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;
 }
 head['Transfer-Encoding']='chunked'
 res.writeHead(200,head)
 fs.open('.'+req.url,'r',function(err,fd){
  fs.fstat(fd,function(err, stats){
   console.log('.'+req.url+' '+stats.size+' '+head['Content-Type']+' '+head['Transfer-Encoding'])
   var buffer = new Buffer(100)
   buf(res,fd,0,stats.size,buffer)
  })
 })
}

http.createServer(app).listen(8000,"127.0.0.1")
console.log('GET http://127.0.0.1:8000/appwsgi/www/index.htm')

我认为我在这里违反了HTTP / 1.1?文本文件确实可以正常工作,但这可能是巧合。我的标题是“200 OK”还是需要它为“100”?一个标题是否足够?

3 个答案:

答案 0 :(得分:12)

如果您正在进行分块传输编码,您实际上需要设置该标头:

Transfer-Encoding: chunked

您可以从google返回的标题中看到,该标题会为主页和其他页面进行分块转移:

HTTP/1.1 200 OK
Date: Sat, 04 Jun 2011 00:04:08 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=f9c65f4927515ce7:FF=0:TM=1307145848:LM=1307145848:S=fB58RFtpI5YeXdU9; expires=Mon, 03-Jun-2013 00:04:08 GMT; path=/; domain=.google.com
Set-Cookie: NID=47=UiPfl5ew2vCEte9JyBRkrFk4EhRQqy4dRuzG5Y-xeE---Q8AVvPDQq46GYbCy9VnOA8n7vxR8ETEAxKCh-b58r7elfURfiskmrOCgU706msiUx8L9qBpw-3OTPsY-6tl; expires=Sun, 04-Dec-2011 00:04:08 GMT; path=/; domain=.google.com; HttpOnly
Server: gws
X-XSS-Protection: 1; mode=block
Transfer-Encoding: chunked

编辑哎呀,读起来太复杂了:

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();
 });
}

你去,一个很好的流式缓冲区供你写。 这是我用不同的方式阅读文件的博客文章。我建议您查看它,以便了解如何在节点的异步环境中最好地处理文件。

答案 1 :(得分:10)

由于Node.js隐式设置' Transfer-Encoding:chunked ',所以我需要在标头中发送的内容类型为charset,如:

'Content-Type': 'text/html; charset=UTF-8'

最初是:

'Content-Type': 'text/html'

......哪些不起作用。指定“ charset = UTF-8 ”会立即强制Chrome呈现分块响应。

答案 2 :(得分:3)

为什么要手动执行所有fs操作?使用fs.createReadStream()函数可能会更好。

最重要的是,我猜是Chrome希望您返回206响应代码。检查req.headers.range,看看Chrome是否期望返回媒体文件的“范围”。如果是,那么您只需要发回Web浏览器请求的文件部分。

为什么重新发明轮子?有很多节点模块可以为您做这类事情。尝试Connect / Express'static中间件。祝你好运!

相关问题