Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ampersand-express-router

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ampersand-express-router - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

9

package.json
{
"name": "ampersand-express-router",
"description": "Serverside backbone router inspired by backbone router and ampersand-router.",
"version": "0.0.2",
"version": "0.0.3",
"author": {

@@ -27,4 +27,5 @@ "name": "Lally Elias",

"router",
"history",
"backbone"
"backbone",
"filters",
"before filters"
],

@@ -43,2 +44,2 @@ "license": "MIT",

}
}
}

@@ -40,2 +40,11 @@ # ampersand-express-router

// ------- BEFORE FILTERS DEFINITIONS ---------
before_filters: {
'users|post': 'beforeCreate',
'users|delete': 'beforeDestroy',
'users|put': 'beforeEdit',
'users|get': 'beforeAll',
'users/:id|get': 'beforeShow'
},
// ------- ROUTE HANDLERS ---------

@@ -66,4 +75,42 @@ create: function(request, response) {

});
},
// ------- BEFORE FILTER HANDLERS ---------
beforeCreate: function(request, response, next) {
request.out = {
name: 'before create'
}
next();
},
beforeDestroy: function(request, response, next) {
request.out = {
name: 'before destroy'
}
next();
},
beforeEdit: function(request, response, next) {
request.out = {
name: 'before edit'
}
next();
},
beforeAll: function(request, response, next) {
request.out = {
name: 'before all'
}
next();
},
beforeShow: function(request, response, next) {
request.out = {
name: 'before show'
}
next();
}
...

@@ -93,2 +140,7 @@ });

before_filters: {
"users|get": "all", // this filter will run before /users
"users/:id|get": "show", // this filter will run before /users/11
},
all: function(request,response,next) {

@@ -100,2 +152,12 @@ //...

//...
},
beforeAll: function(request,response,next) {
//...
next(); //dont forget to call next()
},
beforeShow: function(request,response,next) {
//...
next(); //dont forget to call next()
}

@@ -111,2 +173,7 @@

### routers `router.before_filters`
The before filters hash maps URLs with parameters to functions on your router. Route filters can contain path parameter parts, `:param`.
### constructor / initialize `new Router([options])`

@@ -116,8 +183,8 @@

## credits
## Credits
All credit goes to Jeremy Ashkenas and the rest of the Backbone.js authors.
## license
## License
MIT

@@ -7,6 +7,8 @@ var class_extend = require('ampersand-class-extend');

* @module Router
* @version 0.0.1
* @version 0.0.3
*
* @requires module:express
* @requires module:underscore
* @requires module:ampersand-class-extend
*
* @param {Object} options options to be passed into the router

@@ -36,17 +38,18 @@ *

//internal express router for this router
this._express_router = express.Router({
caseSensitive: options.caseSensitive || false,
strict: options.strict || false,
mergeParams: options.mergeParams || false
});
//call initialize logic
this.initialize.apply(this, arguments);
//bind before filters
this._bind_before_filters(options);
//bind routes to this router
this._bind_routes();
this._bind_routes(options);
//mount to express app
this.app.use(this._express_router);
if (this._express_filter_router) {
this.app.use(this._express_filter_router, this._express_router);
} else {
this.app.use(this._express_router);
}
};

@@ -77,8 +80,21 @@

/**
* @function
*
* compute a request filter function for the specified route
* @param {String} filter a filter to lookup its request filter
* @return {Function} a request filter
*/
_get_filter: function(filter) {
var filter = this.before_filters[filter];
return this[filter];
},
/**
* @function
*
* Bind all defined routes to express router.
*/
_bind_routes: function() {
_bind_routes: function(options) {
if (!this.routes) {

@@ -92,2 +108,10 @@ throw Error("No routes specified for this router ");

//internal express router for this router
this._express_router = express.Router({
caseSensitive: options.caseSensitive || false,
strict: options.strict || false,
mergeParams: options.mergeParams || false
});
this.routes = _.result(this, 'routes');

@@ -106,4 +130,39 @@

});
},
/**
* @function
*
* bind request before filters
*/
_bind_before_filters: function(options) {
this.before_filters = _.result(this, 'before_filters');
if (this.before_filters) {
//internal express router that will be used to organize before filters
//and used in middleware chain
this._express_filter_router = express.Router({
caseSensitive: options.caseSensitive || false,
strict: options.strict || false,
mergeParams: options.mergeParams || false
});
var self = this;
Object.keys(this.before_filters).forEach(function beforeFilter(beforeFilter) {
var method = beforeFilter.split('|')[1] || 'get';
var url = beforeFilter.split('|')[0];
self._express_filter_router[method]('/' + url, function(request, response, next) {
self._get_filter(beforeFilter)(request, response, next);
});
});
}
}
});
/**

@@ -114,2 +173,2 @@ * @function

*/
Router.extend = class_extend;
Router.extend = class_extend;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc