Create a Router in Node.js using crossroads

Routers are very important in any web server. Primary function of a Router is to accept request and on basis of requested URL trigger desired process.

Consider a rough example in that client is requesting

http://abc.com/users/myprofile and there request may serve from URL http://abc.com/node/2 assuming user is stored in node with id 2.

image

In Node.js you can create router using many publicly available modules. Some of them are as follows,

  • Express
  • Director
  • Bouncy
  • Backbone
  • Crossroads

In this post let us create a router using Crossroads. To start with install crossroads module in application using npm crossroads or you may want to use mange modules in Visual Studio as well.

Once crossroads module is installed you need to load crossroads module in application.

image

You can add route in application using addRoute function. I have added route which takes two parameters.


crossroads.addRoute('/{type}/:id:',function(type,id){

 if(!id)
 {
 result = "Acess informtion about all " + type;
 return;
 }
 else
 {
 result = "Acess informtion about "+ type + "with id" +id ;
 return;
 }

 });

In above route

  • Type is parameter need to be passed to request server
  • Id is optional parameter to be passes to request server. Optional parameters should be enclosed with ::

Second parameter of addRoute is a callback function. In this callback you need to write logic to work with different requests with different parameters in request. As you see in above example we are constructing string on basis of requested URL.

If you write result in response then you will get output as below,

image

image

In first request we are passing only type (student) whereas in second request we are passing both type and optional id.

So when adding route you can have parameters in request as

image

If you want to add a route with REST segment then you can do that as follows,

clip_image002

Now on requesting server you will get id passed as optional REST segment.

image

Putting all together you can create a Router in Node.js application using crossroad as below,


var http = require('http');
var crossroads = require('crossroads');
var result ;
crossroads.addRoute('/{type}/:id*:',function(type,id){

 if(!id)
 {
 result = "Acess informtion about all " + type;
 return;
 }
 else
 {
 result = "Acess informtion about "+ type + "with id" +id ;
 return;
 }

 });

http.createServer(function(request,response){
 crossroads.parse(request.url) ;
 response.writeHead(200);
 response.write(result);
 request.on('end',function(){
 response.end("Req End");
 });

}).listen(8080,function(){

 console.log("server started");

 });

In above code apart from creating server we are parsing request URL as well using parse function

clip_image001

In this you can create a router. Off course you can use other modules to create router as well. When working with web applications router of express framework is very powerful. I hope you find this post useful. Thanks for reading.

10 responses to “Create a Router in Node.js using crossroads”

  1. […] Create a Router in Node.js using crossroads (Dhananjay Kumar) […]

  2. Do you believe your posts are really helping any working developers?

  3. Dhananjay Kumar

    Yes I believe ! 🙂 feedback from many developers motivate me to keep going. and that motivation yielded around 800 blog posts 🙂 I am sure it has helped you also !

  4. Debugmode, The problem with your posts is that the contents are not unique.We saw this kind of posts several hundred times in the past. Node has reached five years ago and we had seen this kind of posts many times. Good bloggers will write very less posts, but the content will be unique which will help the real developers. We will get lot of info from framework documentations so that we are expecting different things from blogs.

  5. Alex ,

    I am not aspired to be unique ! I am aspired to be useful :):) I try to convert complex topics in step by step manner which can be used by level 100 or level 200 developers. Let me share a story with you. as you say Node is 5 years old. I agree ! but today I had a webinar on same. There were around 250 people attended webinar. Out of 250 more than 45 to 60 % people were of 5 to 10 years exp. When I asked them maximum of people had no idea what exactly Node is ? How to run a Node program ? etc etc … I have been writing for last 5 years and authores around 800 blog posts. My purpose here not to manifest my knowledge through my blog rather my purpose is to simplify things for developers from my blog. Thanks /DJ

  6. Have you ever thought about writing an e-book or guest authoring
    on other sites? I have a blog based on the same subjects you
    discuss and would really like to have you share some stories/information.
    I know my subscribers would value your work. If you are even remotely
    interested, feel free to shoot me an email.

  7. Hello, of course this post is actually good and I have learned lot
    of things from it about blogging. thanks.

  8. […] For further advanced reading you may want to prefer my other posts on creating a REST API using Express . If you want to create routes using crossroads then you can read my posts on how to create a Router in Node.js using crossroads. […]

  9. Thanks this helped alot, sometime a little demo achives alot

Leave a comment

Create a website or blog at WordPress.com