Thursday, July 03, 2014

Node.js With Express.js Framework - Routing Techniques With Name-space

Express.js Framework on Node.js doesn't inherit from Connect router instead it is built with it.Routing is useful to create Friendly URL's and manage your stuffs in site in elegant manner.When your application becomes complex for managing comes Namespaced routing for managing urls with namespaces However only you must select the routes accordingly for your application.Express.js doesnt force you to use particular routing.As like as An unplanned routing system for CMS will suffer a huge during the production,choose and manage your routes efficiently.

This Post is for 3.x.x version of Express.And note that this is an inbuilt feature in 4.x.x version of express.i will update how to work with 4.x.x routing techniques.


Downloads  |    

Namespace Express routes :

When your application becomes complex you have to manage a big set of uri in your application,however taking it to seperate routes handlers is second part.let us see how could we segregate them as per needs.when you write CMS,Store or anything you need to club the URI for certain functions.Here Namespace comes to recover us.

Expected Namespace URI structure :
when i plan for app store as well as books store within my single website that would be tedious for me to manage i may need URI like below.  --> index page of apps store -->facebook app to download and description --> googledocs app to download and description -->index page of book store ->headerfirst android book for download. --> must load details for admin to edit the items on index page.

Installation (package.json):

"name": "routersample",
"description": "simple routing and namespaced routing",
"version": "0.0.3",
"private": true,
"express": "3.x",
"express-namespace": "*"

Run your npm install command in your application directory.Learn more here

Defining Routes :

Note Here we are initializing the http and express before namespace and then app is initialized after namespaced.unless your app with namespace wont work for your needs.

var http = require("http");
var express = require("express");
var namespace = require("express-namespace");
var app = express();

Defining routes as per our needs goes below :

   app.namespace('/apps', function() {
       app.get('/', function(req, res) {
         res.send('Respond Client with Apps Index Page here');

        app.get('/facebook', function(req, res) {
           res.send('Show Facebook App with download link and description');

       app.get('/googledocs', function(req, res) {
           res.send('Show Google Docs App for download link and description');
       app.delete('/delete/:apps', function(req, res) {
           res.send('delete application with  requested ' + req.params.apps);

 app.namespace('/books', function() {

    app.get('/', function(req, res) {
       res.send('Respond The Client With Books Index Page');

    app.get('/headfirstandroid', function(req, res) {
       res.send('show Head First Android book To the User with Relevant Information');
 app.get('/harrypotter', function(req, res) {
        res.send('show Harry Potter Book To the User with Relevant Information');

Namespace can be nested with one another too.and note that app.router must be loaded explicitly before starting namespace routes.And initialize the namespace routes before the app gets initialize.
Now point your browser url respective to your routes it works beautifully.This is why we call express as bloat free framework

basic routing (video) :

basic form submission and routing files are available above. If you can find any bugs/Errors in my Demo or above code just disqus with me in comments or mail me [email protected] is care.


Post a Comment

feel free to post your comments! Don't Spam here!