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).
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);
With fs.readFile
, I cannnot reproduce :
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);
Did I do something wrong?