From 1855a86d0409069e25c3c87725d5e0eb1bf556eb Mon Sep 17 00:00:00 2001 From: armanmikoyan Date: Mon, 27 Jul 2026 14:19:52 +0400 Subject: [PATCH] feat: add app-aware router creation --- lib/application.js | 17 +++++++++++++++++ test/app.createRouter.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 test/app.createRouter.js diff --git a/lib/application.js b/lib/application.js index 310e6dfef21..09c1718adac 100644 --- a/lib/application.js +++ b/lib/application.js @@ -605,6 +605,23 @@ app.listen = function listen() { return server.listen.apply(server, args) } +/** + * Create a router that inherits this application's routing settings. + * + * @param {Object} [options] + * @return {Router} + * @public + */ + +app.createRouter = function createRouter(options) { + options = Object.assign({ + caseSensitive: this.enabled('case sensitive routing'), + strict: this.enabled('strict routing') + }, options) + + return new Router(options) +} + /** * Log error using console.error. * diff --git a/test/app.createRouter.js b/test/app.createRouter.js new file mode 100644 index 00000000000..c754206f295 --- /dev/null +++ b/test/app.createRouter.js @@ -0,0 +1,40 @@ +'use strict' + +var assert = require('node:assert') +var express = require('../') + +describe('app.createRouter()', function () { + it('should inherit routing settings from the app', function () { + var app = express() + + app.enable('case sensitive routing') + app.enable('strict routing') + + var router = app.createRouter() + + assert.strictEqual(router.caseSensitive, true) + assert.strictEqual(router.strict, true) + }) + + it('should allow options to override inherited settings', function () { + var app = express() + + app.enable('case sensitive routing') + app.enable('strict routing') + + var router = app.createRouter({ + caseSensitive: false, + strict: false + }) + + assert.strictEqual(router.caseSensitive, false) + assert.strictEqual(router.strict, false) + }) + + it('should pass other options to the router', function () { + var app = express() + var router = app.createRouter({ mergeParams: true }) + + assert.strictEqual(router.mergeParams, true) + }) +})