Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created November 26, 2012 13:47
Show Gist options
  • Save shigeki/4148268 to your computer and use it in GitHub Desktop.
Save shigeki/4148268 to your computer and use it in GitHub Desktop.
大容量ファイルのダウンロードを提供するサンプルコード
var fs = require('fs');
var http = require('http');
var util = require('util');
var file = './1G.file';
var stat = fs.statSync(file);
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'octet-stream/binary',
'Content-Length': stat.size
});
var rStream = fs.createReadStream(file);
rStream.on('data', function(chunk) {
rStream.pause();
process.nextTick(function() {
res.write(chunk);
});
});
rStream.on('end', function() {
res.end();
});
res.on('drain', function() {
rStream.resume();
});
}).listen(1337);
setInterval(function() {
console.log(util.inspect(process.memoryUsage()));
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment