Guest User

Untitled

a guest
Jan 23rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. function Router(obj) {
  2. this.methods = obj;
  3. this.route(obj.routes);
  4. }
  5.  
  6. Router.prototype.route = function(obj){
  7. var self = this
  8. , routes = Object.keys(obj);
  9.  
  10. Object.keys(obj).forEach(function(route){
  11. var parts = route.split(' ')
  12. , method = parts.shift().toLowerCase()
  13. , path = parts.shift()
  14. , fn = self.methods[obj[route]];
  15.  
  16. app[method]('/' + path, function(req, res, next){
  17. var args = req.route.keys.map(function(key){
  18. return req.params[key.name];
  19. });
  20. fn.apply(res, args);
  21. });
  22. });
  23. };
  24.  
  25.  
  26. Router.extend = function(obj){
  27. return new Router(obj);
  28. };
Add Comment
Please, Sign In to add comment