Last active
August 29, 2015 14:00
-
-
Save rook2pawn/55662dff09fb539c9c05 to your computer and use it in GitHub Desktop.
Simple Chain Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var methods = require('methods') | |
var routes = {} | |
var setter = function(method) { | |
return function() { | |
var args = [].concat.apply({},arguments).slice(1); | |
routes[method.toUpperCase()][args[0]] = args.slice(1); | |
} | |
} | |
var next = function() { | |
routes[this.req.method][this.req.url][++this.index](this.req,this.res,next.bind({req:this.req,res:this.res,index:this.index})); | |
} | |
var handle = function(req,res) { | |
if (routes[req.method][req.url]) | |
routes[req.method][req.url][0](req,res,next.bind({req:req,res:res,index:0})); | |
} | |
methods.forEach(function(method) { | |
routes[method.toUpperCase()] = {} | |
handle[method] = setter(method) | |
}) | |
module.exports = exports = handle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this here's an example of it