Nest: Get reliable server url with `NestApplication.url`

Created on 23 Sep 2019  路  5Comments  路  Source: nestjs/nest

Feature Request

Is your feature request related to a problem? Please describe.

As a user I want to reliably get the server URL.

Describe the solution you'd like

After app.listen the application should know all the needed information.
I would expect the following behavior.

const app = await NestFactory.create(AppModule);
await app.listen(3000);
console.log(app.url) // returns http://[::1]:3000/

What is the motivation / use case for changing the behavior?

To further improve the DX of NestJS I would like to see the server URL logged in the console once I boot up the application. I would suggest the log message should be part of the boilerplate when running nest new [NAME].

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
  console.log(`Server is listening on ${app.url}`);
}
bootstrap();

Open topics

  • How should we determine when the server is running with the http:// or https:// protocol?
  • [Follow-up] Where should the log message have its place? The options I come up with are:

    • In the nest new-schematics (as described above)

    • Underlying in NestApplication#listen

    • Let nest start handle the printing of the message (maybe the way to go, in case nest start --watch should change the port)

type

Most helpful comment

Getter/method will be enough (without the log)

All 5 comments

I think NestApplication#url should similar to Loobpacks implementation:

https://github.com/strongloop/loopback-next/blob/003180554f3b119ddc335366facd215e11a61a4a/packages/http-server/src/http-server.ts#L137-L154

(FYI: this._address in the code is Http.Server#address() provided by the Node HTTP package. See here)

Sounds useful :) PRs are welcome!

I think this is something I can take on. Do you want the log of listening on to be a part of the listen function, or do you want it to still be up to the dev to add in the log?

Getter/method will be enough (without the log)

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mishelashala picture mishelashala  路  3Comments

rlesniak picture rlesniak  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments

breitsmiley picture breitsmiley  路  3Comments

anyx picture anyx  路  3Comments