Skip to content
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

Add app.createRouter function #3623

Closed
wants to merge 1 commit 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
23 changes: 23 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,29 @@ app.listen = function listen() {
return server.listen.apply(server, arguments);
};

/**
* Creates a router that inherits options from app.settings
* and allows them to be overriden by passing in an options
* object
*
* @param {Object} [options]
* @return {router}
* @public
*/
app.createRouter = function (options) {
var opts = options || {}

if (opts.strict === undefined) {
opts.strict = this.enabled('strict routing')
}

if (opts.caseSensitive === undefined) {
opts.caseSensitive = this.enabled('case sensitive routing')
}

return Router(opts)
}

Copy link
Member

Choose a reason for hiding this comment

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

This is mostly just a style thing, so if there is strong opposition feel free to ignore me, but I find that reducing the code required to achieve the same result often reduces maintenance and understanding costs in the future. For example, this could be written much more succinctly as follows:

app.createRouter = function (options) {
  var opts = options || {}
  opts.strict = options.strict !== undefined ? options.strict : this.enabled('strict routing')
  opts.caseSensitive = options.caseSensitive !== undefined ? options.caseSensitive : this.enabled('case sensitive routing')
  return Router(opts)
}

Sometimes more lines of code is a good thing for clarity, but in this case, there are a bunch of lines which can be achieved with one usage of a ternary. This also has the upside of not unnecessarily calling this.enabled if you pass the option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that we don't have to call this.enabled if the options are passed in, that's good. But to me this looks like a jumbled mess. It takes multiple seconds to decode what it actually means while undefined checks are really easy to make sense of imo.

Copy link
Member

Choose a reason for hiding this comment

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

Like I said, mostly a style thing :), but maybe you can find a middle ground without the unnecessary calls?

Also, technically it is just the same undefined checks you had but in a single line. Maybe other people like the more verbose syntax with the same result, I don't know and it probably shouldn't be the focus of any more energy.

/**
* Log error using console.error.
*
Expand Down
30 changes: 30 additions & 0 deletions test/app.createRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var express = require('../')
var assert = require('assert')

describe('app.createRouter()', function () {

it('should create a router that inherits settings from app.settings', function () {
var app = express()
app.set('strict routing', true)
app.set('case sensitive routing', true)

var router = app.createRouter()

assert(router.strict, 'Router did not inherit strict option from app.settings')
assert(router.caseSensitive, 'Router did not inherit case sensitive option from app.settings')
})

it('should allow options param to override app.settings when creating router', function () {
var app = express()
app.set('strict routing', true)
app.set('case sensitive routing', false)

var router = app.createRouter({
strict: false,
caseSensitive: true
})

assert.equal(router.strict, false, 'options param strict did not override app.settings')
assert.equal(router.caseSensitive, true, 'options param caseSensitive did not override app.settings')
})
})