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(core): add getUrl method to nest-applicaiton #3147

Merged
merged 3 commits into from
Oct 31, 2019
Merged

feat(core): add getUrl method to nest-applicaiton #3147

merged 3 commits into from
Oct 31, 2019

Conversation

jmcdo29
Copy link
Member

@jmcdo29 jmcdo29 commented Oct 10, 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, there is no default behavior for consistently getting the server's url.
Issue Number: #3010

What is the new behavior?

After running await app.listen(<port>) you can add const url = await app.getUrl() and you will receive back a server address in the form of http(s)://[::1]:3000 or http(s)://127.0.0.1:3000 depending on if the server is inherently running on IPv6 or IPv4.

By default, Express runs on IPv6 and Fastify runs on IPv4, so both methods needed to be accounted for. The method needs to be async as it needs to wait to ensure that the server is listening before returning the url, otherwise the method for getting the server's address could return undefined.

This is the same implementation that Loopback uses (that @BrunnerLivio mentioned in the issue).

Does this PR introduce a breaking change?

[] Yes
[x] No

Other information

I couldn't seem to find where the tests were written for nest-application in the core package, but I will be happy to write some for this if need be. I tested this on my local machine with both Fastify and Express, and for both servers with using the default listen() function and with passing in an address to listen at. Please also let me know if you would like any documentation updates.

@coveralls
Copy link

coveralls commented Oct 10, 2019

Pull Request Test Coverage Report for Build 4647

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 19 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.08%) to 95.115%

Files with Coverage Reduction New Missed Lines %
packages/core/injector/container.ts 2 89.83%
packages/core/injector/instance-wrapper.ts 2 86.38%
packages/common/pipes/validation.pipe.ts 3 89.33%
packages/microservices/client/client-rmq.ts 4 87.63%
packages/core/injector/module.ts 8 86.89%
Totals Coverage Status
Change from base Build 3985: -0.08%
Covered Lines: 4342
Relevant Lines: 4565

💛 - Coveralls

Copy link
Member

@BrunnerLivio BrunnerLivio left a comment

Choose a reason for hiding this comment

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

I couldn't seem to find where the tests were written for nest-application in the core package

Currently there are no NestApplication unit and (direct) integration tests (yep, we should definitely add some).
I think most importantly for this feature we should add integration tests.
I'd suggest you add a new folder mkdir -p integration/nest-application/get-url and add your test-project in there.

In terms of documentation, I think none is needed. In the future this will be auto-documented by the API doc compiler from docs.nestjs.com :)

@kamilmysliwiec: Do you agree with that?

@@ -237,6 +237,44 @@ export class NestApplication extends NestApplicationContext
});
}

public async getUrl(): Promise<string> {
return new Promise((resolve, reject) => {
this.httpServer.on('listening', () => {
Copy link
Member

Choose a reason for hiding this comment

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

If the server is not in listening-state it would not reject the promise. Could you add an error message, that the user should call .listen before calling .getUrl?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll get the error message added in once I've got some more time to work on the functionality, great catch!

Copy link
Member Author

Choose a reason for hiding this comment

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

I had to add in a boolean value to the NestApplication class to successfully determine in the app.listen method had been called. Express gives the HTTP server's _handle property a value but fastify doesn't. Otherwise, a reject is called and reason is given. I wasn't sure if you wanted it as a this.logger.error() and process.exit(1) or a reject so currently it logs and rejects. That can easily be updated.

@jmcdo29
Copy link
Member Author

jmcdo29 commented Oct 10, 2019

Currently there are no NestApplication unit and (direct) integration tests (yep, we should definitely add some).

Ah, okay, that definitely explains it.

I'd suggest you add a new folder mkdir -p integration/nest-application/get-url and add your test-project in there.

All right! I'll get that added in with the error message

In terms of documentation, I think none is needed. In the future this will be auto-documented by the API doc compiler from docs.nestjs.com :)

Cool! Was just following the bases of the Pull request template.

@jmcdo29
Copy link
Member Author

jmcdo29 commented Oct 15, 2019

I've added in tests under integration/nest-application/get-url like Livio suggested. Fastify by default works with IPv4, hence why there is not an IPv6 test for it, but there is for Express. Let me know if I can do anything else to enhance the PR. 👍

Copy link
Member

@BrunnerLivio BrunnerLivio left a comment

Choose a reason for hiding this comment

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

LGTM. Well done @jmcdo29 :)

@jmcdo29
Copy link
Member Author

jmcdo29 commented Oct 15, 2019

@BrunnerLivio sorry, I didn't mean to re-request the review. Can't see a way to retract that

@kamilmysliwiec kamilmysliwiec added this to the 6.9.0 milestone Oct 31, 2019
@kamilmysliwiec kamilmysliwiec changed the base branch from master to 6.9.0 October 31, 2019 12:30
@kamilmysliwiec kamilmysliwiec merged commit 19677b5 into nestjs:6.9.0 Oct 31, 2019
@kamilmysliwiec
Copy link
Member

Thank you!

@jmcdo29 jmcdo29 deleted the get-app-url branch October 31, 2019 15:48
@kamilmysliwiec kamilmysliwiec mentioned this pull request Nov 3, 2019
3 tasks
@lock
Copy link

lock bot commented Jan 30, 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 Jan 30, 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.

5 participants