Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Node Server using streams to pipe html content to response (async)
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
// Specify html content type
res.writeHead(200, { 'Content-Type': 'text/html' });
// Replaced readFileSync and end with createReadStream.
// Read contents of index.htm and pipe to writeable stream res.
// Keeps buffer small, sends data a chunk at a time
// to res stream, thereby improving performance
// and avoiding synchronous operation.
fs.createReadStream(__dirname + '/index.htm').pipe(res);
}).listen(1337, '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment