@@ -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