3

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?

Jazz
  • 5,747
  • 5
  • 43
  • 55

2 Answers2

1

It was this issue: https://github.com/joyent/node/issues/2198

Fixed in Node 0.6.4!

Jazz
  • 5,747
  • 5
  • 43
  • 55
0

I had very similar problem with piping (might be related) Valums file-uploader on nodejs - multiple file upload

Also had 2min sleeps. Didn't found solution with pipe did some workaround.

Community
  • 1
  • 1
rskuja
  • 571
  • 4
  • 17