Repository navigation
feat: add app.listRoutes with sub-application support #7531
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
|
@@ -257,6 +266,27 @@ app.route = function route(path) { | |
| return this.router.route(path); | ||
| }; | ||
|
|
||
| /** | ||
| * 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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion · side-effect The code-check · lang-javascript, skeptic |
||
| return this.router.listRoutes(); | ||
| }; | ||
|
|
||
| /** | ||
| * Register the given template engine callback `fn` | ||
| * as `ext`. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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", | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
code-check · architect, pragmatist, skeptic |
||||||
| "send": "^1.1.0", | ||||||
| "serve-static": "^2.2.0", | ||||||
| "statuses": "^2.0.1", | ||||||
|
|
||||||
| 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() {} |
There was a problem hiding this comment.
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.
code-check · doc-skeptic