node.js - Serving files concurrently to multiple clients in node -


i'm building simple http server serves large file clients using streams. need serve multiple clients @ same time, , i'm wondering simplest way achieve is.

my initial feeling using cluster module , forking {num cpus} processes might simplest way.

var streambrake = require('streambrake'); var http = require('http'); var fs = require('fs');  var server = http.createserver(function(req, res) {   var stream = fs.createreadstream('/data/somefile');   stream.pipe(new streambrake(10240)).pipe(res); });  server.listen(1234, 10); 

edit: clarify, issue code won't begin serve second client until has finished serving first.

after thinking issue, i'm confused why believe there's issue. createreadstream asynchronous. here in example complicates code little bit in order demonstrate using createreadstream can indeed service multiple connections @ time.

/*jshint node:true*/ var http = require('http'); var fs = require('fs'); var activerequests = 0; var launchfiverequests = 5;  http.createserver(function (req, res) {      activerequests++;      console.log('request received');      var readstream = fs.createreadstream('play.js', {'buffersize': 1024});      readstream.setencoding('utf8');      readstream.on('data', function (data) {         console.log(activerequests);         res.write(data);     });      readstream.on('end', function () {         res.end();         console.log('end');         activerequests--;     });  }).listen(8080);  var options = {     hostname: 'localhost',     port: 8080,     path: '/' };  while(launchtenrequests--) {     http.get(options, function(res) {         console.log('response received');     }); } 

serving sufficiently large file, , should see 5 requests live @ once, , end ending relatively simultaneously.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -