node.js - respond to each request without having to wait until current stream is finished -
i'm testing streaming creating basic node.js app code streams file response. using code here , here.
but if make request http://127.0.0.1:8000/, open browser , request file, second file not start download until first 1 finished. in example created 1gb file. dd if=/dev/zero of=file.dat bs=1g count=1
but if request 3 more files while first 1 downloading, 3 files start downloading simultaneously once first file has finished.
how can change code respond each request it's made , not have wait current download finish?
var http = require('http'); var fs = require('fs'); var = 1; http.createserver(function(req, res) { console.log('starting #' + i++); // line opens file readable stream var readstream = fs.createreadstream('file.dat', { buffersize: 64 * 1024 }); // wait until know readable stream valid before piping readstream.on('open', function () { console.log('open'); // pipes read stream response object (which goes client) readstream.pipe(res); }); // catches errors happen while creating readable stream (usually invalid names) readstream.on('error', function(err) { res.end(err); }); }).listen(8000); console.log('server running @ http://127.0.0.1:8000/');
your code seems fine way is. checked node v0.10.3 making few requests in multiple term sessions:
$ wget http://127.0.0.1:8000 two requests ran concurrently. same result when using 2 different browsers (i.e. chrome & safari). further, can concurrent downloads in chrome changing request url slightly, in:
http://localhost:8000/foo and
http://localhost:8000/bar the behavior describe seems manifest when making multiple requests same browser same url.
this may browser limitation - looks second request isn't made until first completed or cancelled.
to answer question, if need multiple client downloads in browser
- ensure server code implemented such file-to-url mapping 1-to-many (i.e. using wildcard).
- ensure client code (i.e. javascript in browser), uses different url each request.
Comments
Post a Comment