我正在尝试使用 Connect 来提供静态内容,但是使用大文件(> 40KB) ),发送第一个40,960字节的块(有时是32,940字节),然后传输休眠2分钟,然后传输完成。我发现当我将流传输到响应时会发生这种情况(这就是Connect发送响应的方式)。
I'm trying to use Connect to serve static content, but with large files (> 40KB), a first chunk of 40,960 bytes is sent (sometimes 32,940 bytes), then the transfer sleeps for 2 minutes, then the transfer finishes. I found out that it happens when I pipe a stream to the response (this is how Connect sends the response).
这是在节点0.6上重现这一点的代码.2,在Windows和Linux上,文件为48,980字节:
Here is a code the reproduces this, on Node 0.6.2, on Windows and Linux, with a 48,980 bytes file:
var fs = require( "fs" ), https = require("https"); var privateKey = fs.readFileSync( 'privatekey.pem' ).toString(); var certificate = fs.readFileSync( 'certificate.pem' ).toString(); var options = {key: privateKey, cert: certificate}; var server = https.createServer( options, function( req, res ) { var path = __dirname + "/public" + req.url; fs.stat(path, function(err, stat){ if( err ) { res.writeHead(404, {'Content-Type': 'text/html'}); res.end(""+err); } else { res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': stat.size}); var stream = fs.createReadStream(path); stream.pipe(res); } } ); } ).listen(8364);使用 fs.readFile ,我无法重现:
var fs = require( "fs" ), https = require("https"); var privateKey = fs.readFileSync( 'privatekey.pem' ).toString(); var certificate = fs.readFileSync( 'certificate.pem' ).toString(); var options = {key: privateKey, cert: certificate}; var server = https.createServer( options, function( req, res ) { fs.readFile(__dirname + "/public" + req.url, function(err, data){ if( err ) { res.writeHead(404, {'Content-Type': 'text/html'}); res.end(""+err); } else { res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': data.length}); res.end(data); } } ); } ).listen(8364);我做错了什么?
推荐答案就是这个问题: github/ joyent / node / issues / 2198
已在Node 0.6.4中修复!
Fixed in Node 0.6.4!
更多推荐
Node.js:响应的管道流通过HTTPS冻结
发布评论