Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A very simple router for nodejs
JavaScript CoffeeScript
tree: 5c7958901f

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.markdown
arouter.coffee
arouter.js
package.json

README.markdown

ARouter

Very simple router for node.js

Installation

npm install arouter 

This allows me to define my routes in the following way:

home = function(req,res){
    res.render('index.jade');
}

edit = function(req,res){
    res.render('edit.jade');
}

requireLogin = function(req,res,next){
    if(req.session.user) next();
    else res.redirect('/');
}

edit_p = function(req,res){
    // posting to a page
}
    
routes = {
    get:{
        '/': home,
        '/edit': [requireLogin,edit]
    },
    post:{
        '/edit' : edit_p
    }
}

and then use them in it in the following way

arouter = require('./arouter')
arouter.router(routes,app)

where 'app' is the express app instance.

The middleware you provide in the list for each page, are evaluated from left to right. Hence, anything but the last function needs to have a 'next' function in the parameter to continue the execution. If you're not using any middleware you don't have to put the final function in an array.

If you want to apply a piece (or multiple pieces) of middleware to all of your routing functions just do:

arouter = require('./arouter')
arouter.router(routes,app, Middleware1)
arouter = require('./arouter')
arouter.router(routes,app, [Middleware1, Middleware2])
Something went wrong with that request. Please try again.