Kind: Interface
Source: packages/platform-express/interfaces/serve-static-options.interface.ts
Part of: Platform Express
Interface describing options for serving static assets.
ServeStaticOptions configures how static assets are served by the Express platform integration. It controls cache behavior, index-file resolution, redirects, dotfile access, extension handling, and custom response headers when using static file middleware.
Properties
| Property | Type |
|---|---|
dotfiles | string |
etag | boolean |
extensions | string[] |
fallthrough | boolean |
immutable | boolean |
index | `boolean |
lastModified | boolean |
maxAge | `number |
redirect | boolean |
setHeaders | (res: any, path: string, stat: any) => any |
prefix | string |
Diagram
mermaidgraph LR A[Static asset request] --> B[ServeStaticOptions] B --> C[File resolution] B --> D[Cache headers] B --> E[Redirect and fallthrough behavior] B --> F[Custom setHeaders callback] C --> G[HTTP response] D --> G E --> G F --> G
Usage
tsimport { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useStaticAssets(join(__dirname, '..', 'public'), {
maxAge: '1d',
etag: true,
lastModified: true,
index: ['index.html'],
extensions: ['html'],
fallthrough: true,
redirect: true,
immutable: false,
dotfiles: 'ignore',
setHeaders(res, path, stat) {
if (path.endsWith('.css') || path.endsWith('.js')) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
},
});
await app.listen(3000);
}
bootstrap();
AI Coding Instructions
- Pass
ServeStaticOptionstoNestExpressApplication.useStaticAssets()when configuring Express-hosted static directories. - Use
maxAge,etag,lastModified, andimmutabletogether to define a consistent browser and CDN caching strategy. - Keep
dotfilesrestrictive unless hidden files must be publicly accessible; prefer'ignore'or'deny'over serving them. - Enable
fallthroughwhen unresolved static requests should continue to Nest controllers or other middleware. - Use
setHeadersfor asset-specific headers, but avoid overriding cache headers inconsistently across related file types.
Was this page helpful?