What is @nestjs/serve-static?
@nestjs/serve-static is a package for the NestJS framework that allows you to serve static assets such as HTML, CSS, JavaScript, and image files. It integrates seamlessly with NestJS applications, making it easy to serve static content alongside your API endpoints.
What are @nestjs/serve-static's main functionalities?
Serve Static Files
This feature allows you to serve static files from a specified directory. In this example, the static files are served from the 'public' directory.
const { Module } = require('@nestjs/common');
const { ServeStaticModule } = require('@nestjs/serve-static');
const { join } = require('path');
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'public'), // path to the static files
}),
],
})
export class AppModule {}
Serve Multiple Static Directories
This feature allows you to serve static files from multiple directories. In this example, static files are served from both the 'public' and 'assets' directories.
const { Module } = require('@nestjs/common');
const { ServeStaticModule } = require('@nestjs/serve-static');
const { join } = require('path');
@Module({
imports: [
ServeStaticModule.forRoot([
{ rootPath: join(__dirname, '..', 'public') },
{ rootPath: join(__dirname, '..', 'assets') },
]),
],
})
export class AppModule {}
Serve Static Files with Custom Route
This feature allows you to serve static files with a custom route. In this example, the static files in the 'public' directory are served under the '/static' route.
const { Module } = require('@nestjs/common');
const { ServeStaticModule } = require('@nestjs/serve-static');
const { join } = require('path');
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'public'),
serveRoot: '/static', // custom route
}),
],
})
export class AppModule {}
Other packages similar to @nestjs/serve-static
express-static
The 'express-static' middleware is used in Express.js applications to serve static files. It is similar to @nestjs/serve-static but is designed for use with Express.js rather than NestJS. It provides similar functionality for serving static assets.
koa-static
The 'koa-static' middleware is used in Koa.js applications to serve static files. Like @nestjs/serve-static, it allows you to serve static assets, but it is designed for use with the Koa.js framework.
fastify-static
The 'fastify-static' plugin is used in Fastify applications to serve static files. It provides similar functionality to @nestjs/serve-static but is tailored for the Fastify framework, offering high performance and low overhead.
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
@nestjs/serve-static
package for Nest, useful to serve static content like Single Page Applications (SPA). However, if you are building MVC application or want to serve assets files (images, docs), use the useStaticAssets()
method (read more here) instead.
Installation
$ npm i --save @nestjs/serve-static
Example
See full example here.
Usage
Simply import ServeStaticModule
in your Nest application.
import { Module } from '@nestjs/common';
import { join } from 'path';
import { ServeStaticModule } from '@nestjs/serve-static';
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'client')
})
]
})
export class ApplicationModule {}
API Spec
The forRoot()
method takes an options object with a few useful properties.
Property | Type | Description |
---|
rootPath | string | Static files root directory. Default: "client" |
serveRoot | string | Root path under which static app will be served. Default: "" |
renderPath | string / RegExp | Path to render static app (concatenated with the serveRoot value). Default: * (wildcard - all paths). Note: RegExp is not supported by the @nestjs/platform-fastify . |
exclude | string[] | Paths to exclude when serving the static app. WARNING! Not supported by fastify . If you use fastify , you can exclude routes using regexp (set the renderPath to a regular expression) instead. |
serveStaticOptions | Object | Serve static options (static files) |
useGlobalPrefix | boolean | If true , static app will be prefixed by the global prefix set through setGlobalPrefix() . Default: false https://docs.nestjs.com/faq/global-prefix |
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
License
Nest is MIT licensed.