Created
November 26, 2012 13:47
-
-
Save shigeki/4148268 to your computer and use it in GitHub Desktop.
大容量ファイルのダウンロードを提供するサンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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