Writing A Simple WebServer using NODEJS

Well , we should consider ourselves lucky to be in this age of computing where a programmers job is made very easy with usage of modern languages . Writing a Web Server using C was and is a herculian task . With issues like limited buffer size , spawing threads or new processes to cater new clients and bulky system calls with complex structures to pass as parameters everthing has to be explicitly managed . One of the easiest way which i’ve found out to write webservers is by using NODEJS . Concurrency handling mechanisms are taken care of implicitly and programming becomes easy since Java Script is used .Here is a simple webserver written using NODEJS

var http=require('http'),
	url = require('url'),
	mime = require('mime'),//npm install mime
	fs = require('fs'),
http.createServer(function(req,res) { 
	 pathname = search_path + req.url;

	fs.stat(pathname , function(err,stats){
	  if(err) { 
		res.write('Bad Request , Request cannot be accepted');
	else if(stats.isFile()) {
		var type = mime.lookup(pathname);

		// 200 status - found , no errors 
		res.statusCode = 200;
		//create a readable stream and pipe to the http response 
		var file = fs.createReadStream(pathname);
		file.on("open",function() { 
		file.on("error",function(err) {
	else {
		res.write('Cannot Access Directory');
}).listen(8324);//listen to port 8324
console.log('Server running at 8324');

2 thoughts on “Writing A Simple WebServer using NODEJS

  1. Оh my goodness! Awesome article dude! Мany tҺanks, Howеver I am going throuɡh problеms with
    your RSS. I don’t қnow the reason why І am unable
    tօ join it. Is thеre anybody having simіlar RSS issues?
    Аnyone that ƙnows the solution caan youu kindly respond? Тhanks!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s