Note that there are some explanatory texts on larger screens.

plurals
  1. POrespond to each request without having to wait until current stream is finished
    primarykey
    data
    text
    <p>I'm testing streaming by creating a basic node.js app code that basically streams a file to the response. Using code from <a href="https://stackoverflow.com/a/6928074/442580">here</a> and <a href="http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-read-stream" rel="nofollow noreferrer">here</a>.</p> <p>But If I make a request from <code>http://127.0.0.1:8000/</code>, then open another browser and request another file, the second file will not start to download until the first one is finished. In my example I created a 1GB file. <code>dd if=/dev/zero of=file.dat bs=1G count=1</code></p> <p>But if I request three more files <em>while the first one is downloading</em>, the three files will start downloading simultaneously once the first file has finished.</p> <p><strong>How can I change the code so that it will respond to each request as it's made and not have to wait for the current download to finish?</strong></p> <pre class="lang-js prettyprint-override"><code>var http = require('http'); var fs = require('fs'); var i = 1; http.createServer(function(req, res) { console.log('starting #' + i++); // This line opens the file as a readable stream var readStream = fs.createReadStream('file.dat', { bufferSize: 64 * 1024 }); // This will wait until we know the readable stream is actually valid before piping readStream.on('open', function () { console.log('open'); // This just pipes the read stream to the response object (which goes to the client) readStream.pipe(res); }); // This catches any errors that happen while creating the readable stream (usually invalid names) readStream.on('error', function(err) { res.end(err); }); }).listen(8000); console.log('Server running at http://127.0.0.1:8000/'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload