Skip to content
Open
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
32 changes: 31 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,24 @@ app.use = function use(fn) {
fn.parent = this;

// restore .app property on req and res
router.use(path, function mounted_app(req, res, next) {
function mounted_app(req, res, next) {
var orig = req.app;
fn.handle(req, res, function (err) {
Object.setPrototypeOf(req, orig.request)
Object.setPrototypeOf(res, orig.response)
next(err);
});
}

// Expose the sub-app router without initializing it before mount.
Object.defineProperty(mounted_app, 'router', {
get: function getrouter() {
return fn.router;
}
});

router.use(path, mounted_app);

// mounted an app
fn.emit('mount', this);
}, this);
Expand All @@ -257,6 +266,27 @@ app.route = function route(path) {
return this.router.route(path);
};

/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · docs

Added comments make up 6.8% of the added lines, over the 3% cap (docs-metrics). All 17 comment lines are in this file, and the 13-line JSDoc on a one-line delegating method is most of them. Two points are worth keeping: nested routes are not expanded, and calling the method initializes routers. Cut the block to those two or three lines.

Suggested change
/**
/**
* List routes and mounted routers on this app. Sub-app entries expose `router`; nested routes are not expanded.
* Initializes the routers involved, so set `case sensitive routing` and `strict routing` first.
*
* @return {Array}
* @public
*/

code-check · doc-skeptic

* List the routes and mounted routers registered on this application.
* See Router#listRoutes() documentation for the returned entries.
*
* Mounted sub-applications expose their underlying router in each entry.
* Nested routes are not expanded; call `entry.router.listRoutes()` to
* inspect them.
*
* This method initializes this application's router and any directly mounted
* sub-application routers that have not been initialized yet. Configure
* `case sensitive routing` and `strict routing` on those applications first;
* these settings are captured when each router is initialized.
*
* @return {Array}
* @public
*/

app.listRoutes = function listRoutes() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion · side-effect

The router getter on mounted_app (line 240) returns fn.router. That is the sub-app's lazy getter (line 69), which builds the Router and captures 'case sensitive routing' and 'strict routing' at that moment. So app.listRoutes(), a read-only inspection call, initializes every directly mounted sub-app router (nested sub-apps are not touched), and settings changed on those sub-apps afterwards are silently ignored. The PR describes this as 'preserving lazy initialization', yet calling listRoutes ends lazy initialization for every directly mounted sub-app. Severity is suggestion rather than the warning one reviewer gave. The behavior is deliberate: it is documented in the JSDoc and covered by a test. It also matches existing Express semantics, where any early access to app.router (app.route, app.use) already freezes these settings for the parent app. A side-effect-free inspection path would still remove the order dependence.

code-check · lang-javascript, skeptic

return this.router.listRoutes();
};

/**
* Register the given template engine callback `fn`
* as `ext`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"proxy-addr": "^2.0.8",
"qs": "^6.16.0",
"range-parser": "^1.2.1",
"router": "^2.2.0",
"router": "^2.3.0",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · dependency

The PR description says app.listRoutes() depends on the unmerged pillarjs/router#174 plus a separate wrapper-support commit, and that router is 'temporarily pinned to that Git commit'. The diff does not pin anything. It only changes the range to "router": "^2.3.0". app.listRoutes() calls this.router.listRoutes() (lib/application.js:287), and sub-app entries only expose router if the router package honours the new handler router property. If no published router 2.3.x contains both changes, installing from the registry either fails or gives a router without listRoutes. In that case app.listRoutes() throws a TypeError and test/app.listRoutes.js fails. Severity is warning rather than the critical one reviewer gave: I could not confirm whether such a release exists, because node_modules is not installed in the worktree. The mismatch between the description and package.json is confirmed. Do not merge until router publishes both features and the range points at that release.

Suggested change
"router": "^2.3.0",
"router": "<released version that contains listRoutes and wrapper support>",

code-check · architect, pragmatist, skeptic

"send": "^1.1.0",
"serve-static": "^2.2.0",
"statuses": "^2.0.1",
Expand Down
278 changes: 278 additions & 0 deletions test/app.listRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
'use strict';

var after = require('after');
var assert = require('node:assert');
var methods = require('node:http').METHODS;
var express = require('../');
var request = require('supertest');

describe('app.listRoutes()', function () {
it('should return an empty array for a new application', function () {
var app = express();

assert.deepStrictEqual(app.listRoutes(), []);
});

it('should capture routing settings when first inspecting an empty application', function (done) {
var app = express();
var cb = after(2, done);

assert.deepStrictEqual(app.listRoutes(), []);

app.enable('case sensitive routing');
app.enable('strict routing');
app.get('/Users', function (req, res) {
res.send('users');
});

request(app).get('/users').expect(200, 'users', cb);
request(app).get('/Users/').expect(200, 'users', cb);
});

it('should list routes registered through the application APIs', function () {
var app = express();

app.get('/users', noop);
app.route('/posts').get(noop).all(noop).post(noop);
app.route('/draft');

assert.deepStrictEqual(app.listRoutes(), [
{ path: '/users', methods: ['GET', 'HEAD'], router: undefined },
{ path: '/posts', methods: ['GET', '*', 'POST', 'HEAD'], router: undefined },
{ path: '/draft', methods: [], router: undefined }
]);
});

it('should list the individual HTTP methods registered by app.all()', function () {
var app = express();

app.all('/all', noop);
app.route('/wildcard').all(noop);

assert.deepStrictEqual(app.listRoutes(), [
{ path: '/all', methods: methods, router: undefined },
{ path: '/wildcard', methods: ['*'], router: undefined }
]);
});

it('should expose mounted routers for separate inspection', function () {
var app = express();
var router = express.Router();

router.all('/users', noop);
app.use('/api', router);

var routes = app.listRoutes();

assert.deepStrictEqual(routes, [
{ path: '/api', methods: undefined, router: router }
]);
assert.deepStrictEqual(routes[0].router.listRoutes(), [
{ path: '/users', methods: ['*'], router: undefined }
]);
});

it('should omit plain middleware', function () {
var app = express();

app.use(noop);
app.use('/admin', noop);
app.get('/users', noop);

assert.deepStrictEqual(app.listRoutes(), [
{ path: '/users', methods: ['GET', 'HEAD'], router: undefined }
]);
});

it('should expose the router of a mounted sub-application', function () {
var app = express();
var admin = express();

admin.get('/users', noop);
app.use('/admin', admin);

var routes = app.listRoutes();

assert.deepStrictEqual(routes, [
{ path: '/admin', methods: undefined, router: admin.router }
]);
assert.deepStrictEqual(app.router.listRoutes(), routes);
assert.deepStrictEqual(routes[0].router.listRoutes(), [
{ path: '/users', methods: ['GET', 'HEAD'], router: undefined }
]);
});

it('should preserve registration order and the paths of sub-application mounts', function () {
var app = express();
var admin = express();
var path = /^\/admin/;

app.get('/before', noop);
app.use(admin);
app.use(['/one', ['/two']], admin);
app.use(path, admin);
app.get('/after', noop);

assert.deepStrictEqual(app.listRoutes(), [
{ path: '/before', methods: ['GET', 'HEAD'], router: undefined },
{ path: '/', methods: undefined, router: admin.router },
{ path: '/one', methods: undefined, router: admin.router },
{ path: '/two', methods: undefined, router: admin.router },
{ path: path, methods: undefined, router: admin.router },
{ path: '/after', methods: ['GET', 'HEAD'], router: undefined }
]);
});

it('should allow inspection of nested sub-applications through their routers', function () {
var app = express();
var api = express();
var admin = express();

app.use('/api', api);
api.use('/admin', admin);
admin.get('/users', noop);

var apiRouter = app.listRoutes()[0].router;
assert.strictEqual(apiRouter, api.router);
assert.deepStrictEqual(apiRouter.listRoutes(), [
{ path: '/admin', methods: undefined, router: admin.router }
]);
assert.deepStrictEqual(apiRouter.listRoutes()[0].router.listRoutes(), [
{ path: '/users', methods: ['GET', 'HEAD'], router: undefined }
]);
});

it('should expose sub-applications mounted directly on an express.Router()', function () {
var app = express();
var router = express.Router();
var admin = express();

app.use('/api', router);
router.use('/admin', admin);
admin.get('/users', noop);

assert.deepStrictEqual(app.listRoutes()[0].router.listRoutes(), [
{ path: '/admin', methods: undefined, router: admin.router }
]);
});

it('should reflect routes added after inspecting an empty mounted sub-application', function () {
var app = express();
var admin = express();

app.use('/admin', admin);

var mountedRouter = app.listRoutes()[0].router;
assert.strictEqual(mountedRouter, admin.router);
assert.deepStrictEqual(mountedRouter.listRoutes(), []);

admin.get('/users', noop);

assert.deepStrictEqual(mountedRouter.listRoutes(), [
{ path: '/users', methods: ['GET', 'HEAD'], router: undefined }
]);
});

it('should capture routing settings when first inspecting an empty mounted sub-application', function (done) {
var app = express();
var admin = express();
var cb = after(2, done);

app.use('/admin', admin);
assert.strictEqual(app.listRoutes().length, 1);

admin.enable('case sensitive routing');
admin.enable('strict routing');
admin.get('/Users', function (req, res) {
res.send('users');
});

request(app).get('/admin/users').expect(200, 'users', cb);
request(app).get('/admin/Users/').expect(200, 'users', cb);
});

it('should defer router creation until after mounting and configuration', function (done) {
var app = express();
var admin = express();
var cb = after(3, done);

admin.on('mount', function () {
this.enable('case sensitive routing');
});
app.use('/admin', admin);
admin.enable('strict routing');
admin.get('/Users', function (req, res) {
res.send('users');
});

var mountedRouter = app.listRoutes()[0].router;
assert.strictEqual(mountedRouter, admin.router);
assert.strictEqual(mountedRouter.caseSensitive, true);
assert.strictEqual(mountedRouter.strict, true);

request(app).get('/admin/Users').expect(200, 'users', cb);
request(app).get('/admin/users').expect(404, cb);
request(app).get('/admin/Users/').expect(404, cb);
});

it('should preserve sub-application dispatch and restore the parent application', function (done) {
var app = express();
var admin = express();

app.request.scope = function () { return 'parent'; };
admin.request.scope = function () { return 'child'; };
app.response.scope = function () { return 'parent'; };
admin.response.scope = function () { return 'child'; };

admin.get('/users', function (req, res, next) {
assert.strictEqual(req.app, admin);
assert.strictEqual(res.app, admin);
assert.strictEqual(req.scope(), 'child');
assert.strictEqual(res.scope(), 'child');
res.setHeader('x-subapp', 'hit');
next();
});
app.use('/admin', admin);
app.use(function (req, res) {
assert.strictEqual(req.app, app);
assert.strictEqual(res.app, app);
assert.strictEqual(req.scope(), 'parent');
assert.strictEqual(res.scope(), 'parent');
res.send('parent');
});

assert.strictEqual(app.listRoutes()[0].router, admin.router);

request(app).get('/admin/users')
.expect('x-subapp', 'hit')
.expect(200, 'parent', done);
});

it('should restore the parent application when a sub-application rejects a promise', function (done) {
var app = express();
var admin = express();
var error = new Error('sub-application failure');

admin.get('/users', function (req, res) {
assert.strictEqual(req.app, admin);
assert.strictEqual(res.app, admin);
return Promise.reject(error);
});
app.use('/admin', admin);
app.use(function (err, req, res, next) {
assert.strictEqual(err, error);
assert.strictEqual(req.app, app);
assert.strictEqual(res.app, app);
assert.strictEqual(Object.getPrototypeOf(req), app.request);
assert.strictEqual(Object.getPrototypeOf(res), app.response);
res.status(500).send(err.message);
});

assert.strictEqual(app.listRoutes()[0].router, admin.router);

request(app).get('/admin/users')
.expect(500, 'sub-application failure', done);
});
});

function noop() {}
Loading