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.
Downloads : |
surya.com/apps --> index page of apps store
{
"name": "routersample",
"description": "simple routing and namespaced routing",
"version": "0.0.3",
"private": true,
"dependencies":
{
"express": "3.x",
"express-namespace": "*"
}
}
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 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.
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.
express-js-i-visionblog |
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 them.so i may need URI like below.
surya.com/apps --> index page of apps store
surya.com/apps/facebook -->facebook app to download and description
surya.com/apps/googledocs --> googledocs app to download and description
surya.com/books -->index page of book store
surya.com/books/headfirstandroid ->headerfirst android book for download.
surya.com/books/edit --> 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,
"dependencies":
{
"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.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
0 comments:
Post a Comment