Created
August 20, 2017 21:28
-
-
Save natmegs/66fb99138e6977424355f5e3333b3eb5 to your computer and use it in GitHub Desktop.
Node Server using streams to pipe html content to response (async)
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 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, '127.0.0.1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment