-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Conversation
Pull Request Test Coverage Report for Build 4647
💛 - Coveralls |
There was a problem hiding this 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', () => { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
Ah, okay, that definitely explains it.
All right! I'll get that added in with the error message
Cool! Was just following the bases of the Pull request template. |
I've added in tests under |
There was a problem hiding this 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 :)
@BrunnerLivio sorry, I didn't mean to re-request the review. Can't see a way to retract that |
Thank you! |
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. |
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?
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 addconst url = await app.getUrl()
and you will receive back a server address in the form ofhttp(s)://[::1]:3000
orhttp(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?
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 defaultlisten()
function and with passing in an address to listen at. Please also let me know if you would like any documentation updates.