Skip to content

Commit

Permalink
Scope app middleware to a list of paths
Browse files Browse the repository at this point in the history
Add a new argument to `app.defineMiddlewarePhases` allowing developers
to restrict the middleware to a list of paths or regular expresions.

Examples:

    // A string path (interpreted via path-to-regexp)
    app.middleware('auth', '/admin', ldapAuth);

    // A regular expression
    app.middleware('initial', /^\/~(admin|root)/, rejectWith404);

    // A list of scopes
    app.middleware('routes', ['/api', /^\/assets/.*\.json$/], foo);
  • Loading branch information
Miroslav Bajtoš committed Nov 19, 2014
1 parent 768dc4a commit dbf4d12
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
43 changes: 40 additions & 3 deletions lib/server-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var express = require('express');
var merge = require('util')._extend;
var PhaseList = require('loopback-phase').PhaseList;
var debug = require('debug')('loopback:app');
var pathToRegexp = require('path-to-regexp');

var proto = {};

Expand Down Expand Up @@ -112,18 +113,32 @@ proto.defineMiddlewarePhases = function(nameOrArray) {
/**
* Register a middleware handler to be executed in a given phase.
* @param {string} name The phase name, e.g. "init" or "routes".
* @param {Array|string|RegExp} [paths] Optional list of paths limiting
* the scope of the middleware.
* String paths are interpreted as expressjs path patterns,
* regular expressions are used as-is.
* @param {function} handler The middleware handler, one of
* `function(req, res, next)` or
* `function(err, req, res, next)`
* @returns {object} this (fluent API)
*
* @header app.middleware(name, handler)
*/
proto.middleware = function(name, handler) {
proto.middleware = function(name, paths, handler) {
this.lazyrouter();

if (handler === undefined && typeof paths === 'function') {
handler = paths;
paths = [];
}

if (typeof paths === 'string' || paths instanceof RegExp) {
paths = [paths];
}

assert(typeof name === 'string' && name, '"name" must be a non-empty string');
assert(typeof handler === 'function', '"handler" must be a function');
assert(Array.isArray(paths), '"paths" must be an array');

var fullName = name;
var handlerName = handler.name || '(anonymous)';
Expand All @@ -139,13 +154,15 @@ proto.middleware = function(name, handler) {
if (!phase)
throw new Error('Unknown middleware phase ' + name);

var matches = createRequestMatcher(paths);

var wrapper;
if (handler.length === 4) {
// handler is function(err, req, res, next)
debug('Add error handler %j to phase %j', handlerName, fullName);

wrapper = function errorHandler(ctx, next) {
if (ctx.err) {
if (ctx.err && matches(ctx.req)) {
var err = ctx.err;
ctx.err = undefined;
handler(err, ctx.req, ctx.res, storeErrorAndContinue(ctx, next));
Expand All @@ -157,7 +174,7 @@ proto.middleware = function(name, handler) {
// handler is function(req, res, next)
debug('Add middleware %j to phase %j', handlerName , fullName);
wrapper = function regularHandler(ctx, next) {
if (ctx.err) {
if (ctx.err || !matches(ctx.req)) {
next();
} else {
handler(ctx.req, ctx.res, storeErrorAndContinue(ctx, next));
Expand All @@ -169,6 +186,26 @@ proto.middleware = function(name, handler) {
return this;
};

function createRequestMatcher(paths) {
if (!paths.length) {
return function requestMatcher(req) { return true; };
}

var checks = paths.map(function(p) {
return pathToRegexp(p, {
sensitive: true,
strict: false,
end: false
});
});

return function requestMatcher(req) {
return checks.some(function(regex) {
return regex.test(req.url);
});
};
}

function storeErrorAndContinue(ctx, next) {
return function(err) {
if (err) ctx.err = err;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"loopback-phase": "^1.0.1",
"nodemailer": "~1.3.0",
"nodemailer-stub-transport": "~0.1.4",
"path-to-regexp": "^1.0.1",
"strong-remoting": "^2.4.0",
"uid2": "0.0.3",
"underscore": "~1.7.0",
Expand Down
56 changes: 54 additions & 2 deletions test/app.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var async = require('async');
var path = require('path');

var http = require('http');
Expand Down Expand Up @@ -109,12 +110,58 @@ describe('app', function() {
});
});

it('scopes middleware to a string path', function(done) {
app.middleware('initial', '/scope', pathSavingHandler());

async.eachSeries(
['/', '/scope', '/scope/item', '/other'],
function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) {
if (err) return done(err);
expect(steps).to.eql(['/scope', '/scope/item']);
done();
});
});

it('scopes middleware to a regex path', function(done) {
app.middleware('initial', /^\/(a|b)/, pathSavingHandler());

async.eachSeries(
['/', '/a', '/b', '/c'],
function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) {
if (err) return done(err);
expect(steps).to.eql(['/a', '/b']);
done();
});
});

it('scopes middleware to a list of scopes', function(done) {
app.middleware('initial', ['/scope', /^\/(a|b)/], pathSavingHandler());

async.eachSeries(
['/', '/a', '/b', '/c', '/scope', '/other'],
function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) {
if (err) return done(err);
expect(steps).to.eql(['/a', '/b', '/scope']);
done();
});
});

function namedHandler(name) {
return function(req, res, next) {
steps.push(name);
next();
};
}

function pathSavingHandler() {
return function(req, res, next) {
steps.push(req.url);
next();
};
}
});

describe.onServer('.middlewareFromConfig', function() {
Expand Down Expand Up @@ -600,13 +647,18 @@ describe('app', function() {
});
});

function executeMiddlewareHandlers(app, callback) {
function executeMiddlewareHandlers(app, urlPath, callback) {
var server = http.createServer(function(req, res) {
app.handle(req, res, callback);
});

if (callback === undefined && typeof urlPath === 'function') {
callback = urlPath;
urlPath = '/test/url';
}

request(server)
.get('/test/url')
.get(urlPath)
.end(function(err) {
if (err) return callback(err);
});
Expand Down

0 comments on commit dbf4d12

Please sign in to comment.