Skip to content

expose the matched routes for a request #3100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/static-files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var express = require('../..');
var path = require('path');
var logger = require('morgan');
var app = express();

Expand All @@ -16,25 +17,25 @@ app.use(logger('dev'));
// that you pass it. In this case "GET /js/app.js"
// will look for "./public/js/app.js".

app.use(express.static(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));

// if you wanted to "prefix" you may use
// the mounting feature of Connect, for example
// "GET /static/js/app.js" instead of "GET /js/app.js".
// The mount-path "/static" is simply removed before
// passing control to the express.static() middleware,
// thus it serves the file correctly by ignoring "/static"
app.use('/static', express.static(__dirname + '/public'));
app.use('/static', express.static(path.join(__dirname, 'public')));

// if for some reason you want to serve files from
// several directories, you can use express.static()
// multiple times! Here we're passing "./public/css",
// this will allow "GET /style.css" instead of "GET /css/style.css":
app.use(express.static(__dirname + '/public/css'));
app.use(express.static(path.join(__dirname, 'public', 'css')));

app.listen(3000);
console.log('listening on port 3000');
console.log('try:');
console.log(' GET /hello.txt');
console.log(' GET /js/app.js');
console.log(' GET /css/style.css');
console.log(' GET /css/style.css');
4 changes: 4 additions & 0 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ proto.handle = function handle(req, res, out) {
: layer.params;
var layerPath = layer.path;

// Expose the definition of the route that was hit
req.matchedRoutes = req.matchedRoutes ?
req.matchedRoutes.concat([layer.pathDefinition]) : [layer.pathDefinition];

// this should be done for the layer
self.process_params(layer, paramcalled, req, res, function (err) {
if (err) {
Expand Down
1 change: 1 addition & 0 deletions lib/router/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Layer(path, options, fn) {
this.name = fn.name || '<anonymous>';
this.params = undefined;
this.path = undefined;
this.pathDefinition = path;
this.regexp = pathRegexp(path, this.keys = [], opts);

if (path === '/' && opts.end === false) {
Expand Down
29 changes: 29 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@ describe('Router', function(){
router.handle({ url: '/', method: 'GET' }, { end: done });
});

it('should expose the matched routes on the req object', function (done) {
var router = new Router();
var path = '/foo/:num';

router.use(path, function (req, res) {
assert(req.matchedRoutes);
assert.deepEqual(req.matchedRoutes, [path]);
res.end();
});

router.handle({ url: '/foo/123', method: 'GET' }, {end: done});
});

it('should expose the nested matched routes on the req object', function (done) {
var layer1 = new Router();
var layer2 = new Router();
var layer3 = new Router();

layer1.use(layer2);
layer2.use('/foo/:num', layer3);
layer3.get(/[0-9]/, function (req, res) {
assert.deepEqual(req.matchedRoutes, ['/', '/foo/:num', /[0-9]/]);
res.end();
});

// Simulate request entering "app" or "base" later
layer1.handle({ url: '/foo/123/321', method: 'GET' }, {end: done});
});

describe('.handle', function(){
it('should dispatch', function(done){
var router = new Router();
Expand Down