Skip to content

Commit

Permalink
final optimisation, and typo fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Zolmeister committed Jul 24, 2013
1 parent 7389085 commit 96b201e
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions lib/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,43 +123,51 @@ module.exports = function (sails) {
}

var limitDefault = sails.config.uploadLimitDefault;
var needToCheckRoutes = false;

// pre-calculare regexp for paths
for (var key in sails.config.routes) {
var match = expresstUtils.pathRegexp(key);
sails.config.routes[key].pathRegexp = match;
if (sails.config.routes[key].cores && typeof sails.config.routes[key].cores === 'boolean') {
sails.config.routes[key].cores = '*';
if (sails.config.routes[key].cors && typeof sails.config.routes[key].cors === 'boolean') {
sails.config.routes[key].cors = '*';
}

// Optimization: don't check routes if we don't need to
if(sails.config.routes[key].cors || sails.config.routes[key].limit) {
needToCheckRoutes = true;
}
}

// Limit file uploads and set CORES per route
// Limit file uploads and set CORS per route
app.use(function(req, res, next) {

// Iterate over routes
for (var key in sails.config.routes) {
var route = sails.config.routes[key];

// Check if route matches the url we're trying to go to
if(route.pathRegexp.exec(req.url)) {
if (typeof route === 'object'){

// Apply cores config
if(route.cores){
res.setHeader('Access-Control-Allow-Origin', route.cores);
if(needToCheckRoutes) {
// Iterate over routes
for (var key in sails.config.routes) {
var route = sails.config.routes[key];

// Check if route matches the url we're trying to go to
if(route.pathRegexp.exec(req.url)) {
if (typeof route === 'object'){

// Remove CSRF token if it exists
res.locals._csrf = null;
}

// Apply uploadLimit config
if(route.uploadLimit) {
return express.limit(route.uploadLimit)(req, res, next);
// Apply cors config
if(route.cors){
res.setHeader('Access-Control-Allow-Origin', route.cors);

// Remove CSRF token if it exists
res.locals._csrf = null;
}

// Apply uploadLimit config
if(route.uploadLimit) {
return express.limit(route.uploadLimit)(req, res, next);
}
}

return express.limit(limitDefault)(req, res, next);
}

return express.limit(limitDefault)(req, res, next);
}

}

return express.limit(limitDefault)(req, res, next);
Expand Down

0 comments on commit 96b201e

Please sign in to comment.