Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Router(obj) {
- this.methods = obj;
- this.route(obj.routes);
- }
- Router.prototype.route = function(obj){
- var self = this
- , routes = Object.keys(obj);
- Object.keys(obj).forEach(function(route){
- var parts = route.split(' ')
- , method = parts.shift().toLowerCase()
- , path = parts.shift()
- , fn = self.methods[obj[route]];
- app[method]('/' + path, function(req, res, next){
- var args = req.route.keys.map(function(key){
- return req.params[key.name];
- });
- fn.apply(res, args);
- });
- });
- };
- Router.extend = function(obj){
- return new Router(obj);
- };
Add Comment
Please, Sign In to add comment