@@ -25,6 +25,7 @@ import { MiddlewareModule } from './middleware/middleware-module';
2525import { NestApplicationContext } from './nest-application-context' ;
2626import { Resolver } from './router/interfaces/resolver.interface' ;
2727import { RoutesResolver } from './router/routes-resolver' ;
28+ import { platform } from 'os' ;
2829
2930const { SocketModule } = optionalRequire (
3031 '@nestjs/websockets/socket-module' ,
@@ -237,6 +238,40 @@ export class NestApplication extends NestApplicationContext
237238 } ) ;
238239 }
239240
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 } ` ;
261+ }
262+
263+ private host ( ) : string | undefined {
264+ const address = this . getHttpServer ( ) . address ( ) ;
265+ if ( typeof address === 'string' ) {
266+ return undefined ;
267+ }
268+ return address && address . address ;
269+ }
270+
271+ private getProtocol ( ) : 'http' | 'https' {
272+ return this . appOptions && this . appOptions . httpsOptions ? 'https' : 'http' ;
273+ }
274+
240275 public setGlobalPrefix ( prefix : string ) : this {
241276 this . config . setGlobalPrefix ( prefix ) ;
242277 return this ;
0 commit comments