Add support for conditional loading of middleware #2545
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have a need to be able to install middleware based on the value of configuration.
It could be done with express-noop like this:
That is doing it's job, but it is wasteful, as there is no need to register any middleware if the condition is false. It's only being evaluated up front any way.
It could also be done by using an if sentence.
But it is annoying when you are nesting express applications to build structures:
With this PR you could use
.useif()
instead of use and make it all much more convenient and readable:I hope that I made my motivation and use-case clear, and that the test coverage and code style lives up to your expectations. If there is anything that I can improve, I'll be more than happy to change it.