Skip to content

Commit 4cae28d

Browse files
committed
feat(core): change getUrl to async method
1 parent e8fe17c commit 4cae28d

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

packages/common/interfaces/nest-application.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface INestApplication extends INestApplicationContext {
4848
*
4949
* @returns The IP where the server is listening
5050
*/
51-
url(): string;
51+
getUrl(): Promise<string>;
5252

5353
/**
5454
* Starts the application (can be awaited).

packages/core/nest-application.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ export class NestApplication extends NestApplicationContext
227227
): Promise<any>;
228228
public async listen(port: number | string, ...args: any[]): Promise<any> {
229229
!this.isInitialized && (await this.init());
230-
231230
this.httpAdapter.listen(port, ...args);
232231
return this.httpServer;
233232
}
@@ -238,30 +237,34 @@ export class NestApplication extends NestApplicationContext
238237
});
239238
}
240239

241-
public url(): string {
242-
const address = this.getHttpServer().address();
243-
if (typeof address === 'string') {
244-
if (platform() === 'win32') {
245-
return address;
246-
}
247-
const basePath = encodeURIComponent(address);
248-
return `${this.getProtocol()}+unix://${basePath}`;
249-
}
250-
let host = this.host();
251-
if (address && address.family === 'IPv6') {
252-
if (host === '::') {
253-
host = '::1';
254-
} else if (host === '0.0.0.0') {
255-
host = '127.0.0.1';
256-
}
257-
} else {
258-
host = '127.0.0.1';
259-
}
260-
return `${this.getProtocol()}://${host}`;
240+
public async getUrl(): Promise<string> {
241+
return new Promise((resolve, reject) => {
242+
this.httpServer.on('listening', () => {
243+
const address = this.httpServer.address();
244+
if (typeof address === 'string') {
245+
if (platform() === 'win32') {
246+
return address;
247+
}
248+
const basePath = encodeURIComponent(address);
249+
return `${this.getProtocol()}+unix://${basePath}`;
250+
}
251+
let host = this.host();
252+
if (address && address.family === 'IPv6') {
253+
if (host === '::') {
254+
host = '[::1]';
255+
} else {
256+
host = `[${host}]`;
257+
}
258+
} else if (host === '0.0.0.0') {
259+
host = '127.0.0.1';
260+
}
261+
resolve(`${this.getProtocol()}://${host}:${address.port}`);
262+
});
263+
});
261264
}
262265

263266
private host(): string | undefined {
264-
const address = this.getHttpServer().address();
267+
const address = this.httpServer.address();
265268
if (typeof address === 'string') {
266269
return undefined;
267270
}

0 commit comments

Comments
 (0)