node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey ยป



Very simple router for node.js


npm install arouter	

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

home = function(req,res){

edit = function(req,res){

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

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

and then use them in it in the following way

arouter = require('./arouter')

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])