![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@nestjs/serve-static
Advanced tools
Nest - modern, fast, powerful node.js web framework (@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.
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 {}
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.
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.
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.
@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.
$ npm i --save @nestjs/serve-static
See full example here.
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 {}
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) |
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) |
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.
Nest is MIT licensed.
FAQs
Nest - modern, fast, powerful node.js web framework (@serve-static)
We found that @nestjs/serve-static demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.