Node.js With Express.js Framework - Routing Techniques With Name-space
on
Get link
Facebook
X
Pinterest
Email
Other Apps
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 routes.here 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
environment.so,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.
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.
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.use(app.router); 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'); }); }); http.createServer(app).listen(3000);
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 video.download 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 s.shivasurya@gmail.com.share is care.
Comments
Post a Comment