Both of these snippets work. The only difference is the location of the call to fs.createReadStream().
It seems to me that it would be more efficient to call it once and assign the result to a var, rather than calling it on every request.
What's wrong with my thinking?
Answer provided by learnyounode: is it better than mine? If so, why?
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'content-type': 'text/plain'
});
fs.createReadStream(process.argv[3]).pipe(res)
});
My answer?
var src = fs.createReadStream(process.argv[3])
server = http.createServer(function(request, response) {
res.writeHead(200, {
'content-type': 'text/plain'
})
src.pipe(response);
});
Answering my own question. My code works once, not twice, because the stream object gets consumed by the destination.