11import * as http from 'http' ;
2+ import * as https from 'https' ;
23import * as optional from 'optional' ;
34import * as bodyParser from 'body-parser' ;
45import iterate from 'iterare' ;
@@ -33,6 +34,7 @@ import { RoutesResolver } from './router/routes-resolver';
3334import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception' ;
3435import { MiddlewaresContainer } from './middlewares/container' ;
3536import { NestApplicationContext } from './nest-application-context' ;
37+ import { HttpsOptions } from '@nestjs/common/interfaces/https-options.interface' ;
3638
3739const { SocketModule } =
3840 optional ( '@nestjs/websockets/socket-module' ) || ( { } as any ) ;
@@ -59,12 +61,15 @@ export class NestApplication extends NestApplicationContext
5961 private readonly microservices = [ ] ;
6062 private isInitialized = false ;
6163
62- constructor ( container : NestContainer , private readonly express ) {
64+ constructor (
65+ container : NestContainer ,
66+ private readonly express ,
67+ private readonly httpsOptions : HttpsOptions = null ,
68+ ) {
6369 super ( container , [ ] , null ) ;
6470
65- const modules = this . container . getModules ( ) . values ( ) ;
66- this . contextModule = modules . next ( ) . value ;
67- this . httpServer = http . createServer ( express ) ;
71+ this . selectContextModule ( ) ;
72+ this . httpServer = this . createServer ( ) ;
6873
6974 const ioAdapter = IoAdapter ? new IoAdapter ( this . httpServer ) : null ;
7075 this . config = new ApplicationConfig ( ioAdapter ) ;
@@ -75,6 +80,18 @@ export class NestApplication extends NestApplicationContext
7580 ) ;
7681 }
7782
83+ public selectContextModule ( ) {
84+ const modules = this . container . getModules ( ) . values ( ) ;
85+ this . contextModule = modules . next ( ) . value ;
86+ }
87+
88+ public createServer ( ) : any {
89+ if ( ! this . httpsOptions ) {
90+ return http . createServer ( this . express ) ;
91+ }
92+ return https . createServer ( this . httpsOptions , this . express ) ;
93+ }
94+
7895 public async setupModules ( ) {
7996 this . socketModule && this . socketModule . setup ( this . container , this . config ) ;
8097
@@ -173,7 +190,11 @@ export class NestApplication extends NestApplicationContext
173190 }
174191
175192 public async listen ( port : number | string , callback ?: ( ) => void ) ;
176- public async listen ( port : number | string , hostname : string , callback ?: ( ) => void ) ;
193+ public async listen (
194+ port : number | string ,
195+ hostname : string ,
196+ callback ?: ( ) => void ,
197+ ) ;
177198 public async listen ( port : number | string , ...args ) {
178199 ! this . isInitialized && ( await this . init ( ) ) ;
179200
0 commit comments