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

feat() add host matching support to controllers #2980

Merged
merged 5 commits into from
Jan 24, 2020

Conversation

nonplus
Copy link
Contributor

@nonplus nonplus commented Sep 18, 2019

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Currently the request hostname is not taken into consideration when dispatching routes.

Issue Number: #2521

What is the new behavior?

  • New optional host @Controller setting that changes controller to only route if the request host matches.
  • The host value may capture parameters, e.g. :tenant.example.com
  • New @HostParam parameter decorator that returns captured host parameter, e.g. @HostParam('tenant') tenant: string
  • Caveat: Due to how Fastify implements routing, this feature only works in the default (Express) router. When used with Fastify, routing to a controller with a{ host: ":tenant.example.com" } filter will return a 500 response with an HTTP Adapter does not support filtering on { host: ":tenant.example.com" } message.
@Controller({ host: ':tenant.example.com')
export class TenantController {
  @Get()
  public getInfo(@HostParam('tenant') tenant: string) {
    return `Tenant from hostname: [${tenant}]`;
  }
}

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@coveralls
Copy link

coveralls commented Sep 18, 2019

Pull Request Test Coverage Report for Build 3336

  • 26 of 35 (74.29%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.05%) to 95.235%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/router/router-explorer.ts 11 20 55.0%
Totals Coverage Status
Change from base Build 2285: -0.05%
Covered Lines: 4137
Relevant Lines: 4344

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

Thanks for your contribution! I'll review it shortly.

});
}

private applyHostFilter(host, handler) {
if (!host) {
return (req, res, next) => {
Copy link
Member

Choose a reason for hiding this comment

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

Can't we just return handler here? (without setting the hosts property on req)

@kamilmysliwiec
Copy link
Member

Would you like to create a PR to the docs with this feature? :)

@nonplus
Copy link
Contributor Author

nonplus commented Oct 3, 2019

@kamilmysliwiec, the docs are done via the docs.nestjs.com repo, right? I'll open a PR today.

@nonplus
Copy link
Contributor Author

nonplus commented Oct 3, 2019

PR for documentation update: nestjs/docs.nestjs.com#737

@kamilmysliwiec kamilmysliwiec added this to the 6.10 milestone Nov 3, 2019
@kamilmysliwiec kamilmysliwiec modified the milestones: 6.10, 6.11.0 Dec 27, 2019
@kamilmysliwiec kamilmysliwiec merged commit e3c69b1 into nestjs:master Jan 24, 2020
@kamilmysliwiec
Copy link
Member

Thank you! This will be published as 6.12.0

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants