Creating a Simple http server in node.js to serve video


Overview

Creating a simple http server listening at port 3000 and is used to serve video.

Code


													
//serve-video.js
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
	console.log("Port Number : 3000");
	// change the MIME type to 'video/mp4'
    res.writeHead(200, {'Content-Type': 'video/mp4'});
    fs.exists('video.mp4',function(exists){
		if(exists)
		{
			var rstream = fs.createReadStream('video.mp4');
			rstream.pipe(res);
		}
		else
		{
			res.send("Its a 404");
			res.end();
		}
	});
}).listen(3000);
						
					

													
												

Run

  • Now run the snippet using the following command :
    													
    >node serve-video.js