My first web application in Node js is an implementation of vertex colouring algorithm. Here the application is a graph colouring technique which takes a graph and provide a proper colour to each of nodes according to vertex colouring algorithm. Here the server side and client side program is implemented in javascript. The frontend consisting of HTML5 ,jquery ,javascript .Client program consist of html5 canvas and we can draw a graph using javascript in the canavas.The program code is in one file which conatin both code of html5 and javascript.
In this article i talk about only the server side programming using Node js .Node js is an open source ,low level ,evented ,nonblocking server side javascript.If you want to read more about Node js please go to this link https://neethutv.wordpress.com/2012/02/27/a-simple-chat-server-in-node-js/ and come back. To create this vertex colouring application we need following constraints :
- We want to server web pages so need a HTTP server
- Server need to answer differently to requests ,depending on the which URL the request was asking. So need some kind of router to map requests to request handlers
- To fulfill the requests that arrive at the server and have been routed using the router ,need request handlers
- The router also treat incoming post data such as here the adjacency list of the graph for input to colouring algorithm and give it to the request handlers so need request data handling .Here request handlers mainly contain graph colouring algorithm.
So let us start our application.
A basic HTTP server
I create a main file called index.js which is to start our application and server.js where our HTTP server code lives. Let us start with server module. Server.js conatin following code
var http =require('http');
var url=require("url");
function start(route ,handle) {
http.createServer(function (req, res) {
var pathname=url.parse(req.url).pathname;
console.log(pathname);
var body=route(handle ,pathname,res,req);
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
}
exports.start=start;
The first line requires the http module and that with Node js and make it accessible through the variable http:
var http =require('http');
Also we requires another module named as url. This module have utilities for URL resolution and parsing.
var url=require("url");
Making some code a module means we need to export those parts of its functionality that we want to provide to scripts that require our module. Here our main module is index.js so to load server module we need to add a line as in index.js a follows:
var server=require("./server");
For now our HTTP server need to export ,we add a function named start as in above program and export this function as:
exports.start=start;
The url module provide methods which allow to parse pathname from url:
var pathname=url.parse(req.url).pathname;
Our application can now distinguish requests based on the URL path requested – this allows us to map requests to our request handlers based on the URL path using our (yet to be written) router. Serever code contain another function named as route which will expalin later.
Route The Requests
We need to able to feed requested url and GET and POST parameter into our router, and based on this the router then need to decide which code to execute. So this is the third part of our application which will discuss later. Here is the code for router.js
function route(handle,pathname,res,req) {
console.log("About to route a request for " + pathname);
if(typeof handle[pathname]==='function'){
return handle[pathname](res,req);
}else {
console.log("No request handler found for " + pathname);
}
}
exports.route = route;
This code will explain later.
Routing to request Handlers
Routing means we want to handle requests to different URLs differently.In our application first we need a request for web page to draw a graph. And also we need another url which contain POST data, which is created after submitting the graph for colouring. Router is not the place actually do something with the requests, Here requests are routed to request Handlers. Let us create a module called requestHandlers.js ,it is a placeholder function for every request handler and export these as methods of the module. Here is the code is too big so you can refer my bitbucket repository.
requestHandlers.js mainly contain two methods one is start which is to start the HTML page contain canvas to draw graph. We can load this HTML page using a module named ‘fs’ in Node js. The code as follows ,
var fs=require('fs');
function start(res,req) {
console.log("Request handler 'start' was called.");
fs.readFile('index.html',function (err, data){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end()
});
return ;
}
But we need to pass the request handlers from our server into router, Here we have two handlers. Now we can use javascript object which are just collections of name/value pair ,and these values may be strings ,numbers ,functions. Here we use values as functions.We want to pass the list of requestHandlers as an object.Now let us start to look our main module index.js ,which contain following code
var server=require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/graph"] = requestHandlers.colour;
server.start(router.route, handle);
We can see that handle is a collection of requesthandlers,map urls to corresponding request handlers.So whenever ‘/’ this url enters then execute the start function which resides on requestHandlers.js. We can also map different url’s to same request handler.If ‘/graph’ url enters then the colouring function will perform and return the coloured values.Also we need to use the function route() in server module:
route(handle ,pathname,res,req);
In our server program we have add handle parameter to our start() function and pass the handle object on the route() callback as its first parameter.Now here is the time to explain router.js.We need to check if a request handler for the given pathname exists,and if it does simply call according function.Because we can access our request handler functions from our object just as we would access an element of an associative array, we have his nice fluent handle[pathname]();
Handling the requests
Our requestHandlers.js contain two function one is to load html page which contain canvas to draw graph. After completed the graph just send the adjacency list by jquery/ajax. Then click the submit button another url will created named as ‘/graph’, and this are send through using jquery. Then when ‘/graph’ enters on server program it will execute the colouring algorithm.
Algorithm keeps a set of colours and the ‘availability list’ of colours for each node. First it takes each node in the order. It then checks the adjacentcy list of that node. If the first node in the list is coloured, it deletes that colour from the availability list. Then takes the next node from the adjacentcy list and the process continues. Last assigns the first colour in the availability list. This ensures the algorithm to take smallest number of colours. Then return the result as:
res.write(JSON.stringify(value_color)) ;
Then we can see the coloured graph on our web page. To get the complete code please visit my bitbucket repository https://bitbucket.org/neethuedappal/vertex-colouring-algorithm-using-node-js/changeset/3ed8b6e1599c



















