feature(http-adapter) Added the application's global prefix to error handlers and global middlewares #3656
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.
Added the application's global prefix to the error and not found handlers, and enabled the adapters to register one based on prefix.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Attempting to register two applications on the same adapter leads to an error handler conflict, even if the applications have distinct global prefixes set.
Issue Number: N/A
What is the new behavior?
The error handler is set for the global prefix.
For consistency, cors and body parser are also modified with a prefix.
Does this PR introduce a breaking change?
If someone developed a custom solution to this problem, such as registering the Nest application through a prefixed register() call, they may end up in a situation where the application is prefixed twice. Once by their register call, and a second time by Nest's register call. They would need to remove their solution to remove this double prefixing.
The more typical case of "one application per adapter, with a prefix" is affected, in that if the prefix itself is wrong, the default error handlers would be used. Normal operations, or errors within the application itself would behave as before.
The most typical case of "one application per adapter, without a prefix" is not affected. A prefix "/" is used, which should behave as currently.
Also, the fastify methods now return a void promise that resolves when the registration completes, whereas they previously returned the fastify instance. If someone relied on this behaviour, that would break. Even if they don't, if they call these methods at all, they should await them to ensure they work properly.
Other information
The view engine is not set with a prefix, despite the fact that fastify can do that. The problem is express can't, so for consistency between the adapters, neither one sets it per prefix.