NestExpressApplication

Interface describing methods on NestExpressApplication.


interface NestExpressApplication<TServer extends CoreHttpServer | CoreHttpsServer = CoreHttpServer> extends INestApplication {
  getHttpAdapter(): HttpServer<Express.Request, Express.Response, Express>
  listen(port: string | number, callback?: () => void): Promise<TServer>
  set(...args: any[]): this
  engine(...args: any[]): this
  enable(...args: any[]): this
  disable(...args: any[]): this
  useStaticAssets(options: ServeStaticOptions): this
  enableCors(options?: any): void
  useBodyParser<Options = NestExpressBodyParserOptions>(parser: NestExpressBodyParserType, options?: Omit<Options, "verify">): this
  setBaseViewsDir(path: string | string[]): this
  setViewEngine(engine: string): this
  setLocal(key: string, value: any): this
}

See also

Methods

getHttpAdapter()

Returns the underlying HTTP adapter bounded to the Express.js app.


getHttpAdapter(): HttpServer<Express.Request, Express.Response, Express>

Parameters

There are no parameters.

Returns

HttpServer<Express.Request, Express.Response, Express>

listen()

Starts the application.


listen(port: string | number, hostname: string, callback?: () => void): Promise<TServer>

Parameters

Option Type Description
port string | number
hostname string
callback () => void

Optional. Default is undefined.

Returns

Promise<TServer>

set()

A wrapper function around native express.set() method.


set(...args: any[]): this

Parameters

Option Type Description
args any[]

Examples

    
app.set('trust proxy', 'loopback')

Returns

this

engine()

A wrapper function around native express.engine() method.


engine(...args: any[]): this

Parameters

Option Type Description
args any[]

Examples

    
app.engine('mustache', mustacheExpress())

Returns

this

enable()

A wrapper function around native express.enable() method.


enable(...args: any[]): this

Parameters

Option Type Description
args any[]

Examples

    
app.enable('x-powered-by')

Returns

this

disable()

A wrapper function around native express.disable() method.


disable(...args: any[]): this

Parameters

Option Type Description
args any[]

Examples

    
app.disable('x-powered-by')

Returns

this

useStaticAssets()

Sets a base directory for public assets.


useStaticAssets(path: string, options?: ServeStaticOptions): this

Parameters

Option Type Description
path string
options ServeStaticOptions

Optional. Default is undefined.

Returns

this

enableCors()


enableCors(options?: any): void

Parameters

Option Type Description
options any

Optional. Default is undefined.

Returns

void

useBodyParser()

Register Express body parsers on the fly. Will respect the application's rawBody option.


useBodyParser<Options = NestExpressBodyParserOptions>(parser: NestExpressBodyParserType, options?: Omit<Options, "verify">): this

Parameters

Option Type Description
parser NestExpressBodyParserType
options Omit

Optional. Default is undefined.

Examples

    
const app = await NestFactory.create(
  AppModule,
  { rawBody: true }
);
app.useBodyParser('json', { limit: '50mb' });

Returns

this

setBaseViewsDir()

Sets one or multiple base directories for templates (views).


setBaseViewsDir(path: string | string[]): this

Parameters

Option Type Description
path string | string[]

Examples

    
app.setBaseViewsDir('views')

Returns

this

setViewEngine()

Sets a view engine for templates (views).


setViewEngine(engine: string): this

Parameters

Option Type Description
engine string

Examples

    
app.setViewEngine('pug')

Returns

this

setLocal()

Sets app-level globals for view templates.


setLocal(key: string, value: any): this

Parameters

Option Type Description
key string
value any

Examples

    
app.setLocal('title', 'My Site')

Returns

this