Created
November 16, 2010 23:01
-
-
Save bnoordhuis/702695 to your computer and use it in GitHub Desktop.
node.js + sendfile = file serving++
This file contains hidden or 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
http = require('http'); | |
fs = require('fs'); | |
fd = fs.openSync(__filename, 'r'); | |
size = fs.fstatSync(fd).size; | |
server = http.createServer(function(req, res) { | |
res.writeHead(200, { | |
'Content-Length': size, | |
'Content-Type': 'text/plain' | |
}); | |
res._send(''); // force flush | |
fs.sendfile(req.connection.fd, fd, 0, size, function(err, n) { | |
console.log(arguments); | |
res.end(); | |
}); | |
}); | |
console.log("Starting server."); | |
server.listen(8080); |
Neither. It's in lib/fs.js but undocumented.
It seems like after nodejs 0.6.x, the socket fd has been removed from javascript api. The sample does not works.
I am trying to find linux's "sendfile" system call in node.js, it seems had been implement?~
It was removed in 2012, apparently broken between 2010 and 2012.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could not find the
sendfile
method in the node documentation. Is this pseudo-code or some other library is needed?