Last Updated: February 25, 2016
·
536
· jeffscottward

Node Hello-World - Response Handeling Basics

Node CH.1 Learnings

Node is capable of sending both full payload requests
and "streams" whereby the data is sent back in chunks over time.

var http = require('http');
var fs = require('fs');

http.createServer(function(req,res) {
    res.writeHead(200,{'Content-Type':'image/png'});
    fs.createReadStream('./image.png').pipe(res);
}).listen(3000);

console.log('Server Running at http://localhost:3000/');

You can use a callback that listens for events such as 'data' and 'end' against an object that is created from createReadStream(myFile).

var stream = fs.createReadStream('./image.png').
stream.on('data',function(){
  // Do some awesome everytime a chunk passes through
});