Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
大容量ファイルのダウンロードを提供するサンプルコード
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
You can’t perform that action at this time.