@nestjs/serve-static

Official NestJS module for serving static files and single-page apps from an Express or Fastify server

SDK
npm
v12.0.0
517 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
78 /100 Good
Development Activity 96
Maintenance 72
Community 56
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
77 /100 Good
Architecture 80
Code Quality 78
Innovation 58
Learning Curve 90

@nestjs/serve-static is the official NestJS module for serving static assets — typically a built single-page application — directly from a Nest server without a separate web server or reverse proxy. It plugs into the existing Express or Fastify HTTP adapter that the Nest app already uses, so no extra runtime dependency is added beyond the adapter-specific static-file middleware.

The module detects which underlying HTTP adapter (Express or Fastify) the application is running on via an internal loader abstraction, and registers the appropriate static-serving middleware automatically. It supports serving multiple root paths, custom render paths, and excluding specific routes (such as API prefixes) from being intercepted by the static handler, configurable through both forRoot() and forRootAsync().

What You Get

  • ServeStaticModule.forRoot() and forRootAsync() for static or dependency-driven configuration
  • Automatic detection of the underlying HTTP adapter (Express or Fastify) with dedicated loader implementations for each
  • Support for multiple static root paths registered simultaneously
  • Route exclusion so API endpoints aren’t shadowed by the static file handler
  • A no-op loader fallback so the module degrades gracefully when no supported adapter is present

Common Use Cases

  • Serving a built React/Vue/Angular SPA bundle directly from the same NestJS server that exposes the app’s REST API
  • Deploying a full-stack app as a single process/container without a separate Nginx or static-hosting layer
  • Excluding /api (or similar) route prefixes from static file interception in combined frontend+backend deployments
  • Switching between Express and Fastify adapters without changing static-file-serving configuration

Under The Hood

Architecture ServeStaticModule (in lib/serve-static.module.ts) is a dynamic Nest module implementing OnModuleInit; on init it resolves the app’s HttpAdapterHost and delegates to an AbstractLoader implementation selected by serve-static.providers.ts — ExpressLoader, FastifyLoader, or a NoopLoader fallback when no recognized adapter is present — each loader registering the adapter-native static middleware with the module’s configured root paths and exclusions.

Tech Stack TypeScript module with @fastify/static, express, and fastify all declared as optional peer dependencies (only one is required at runtime depending on the app’s adapter), plus path-to-regexp as the sole hard runtime dependency for route-exclusion pattern matching. Testing uses Vitest with a full e2e suite under tests/e2e/ exercising both express-adapter.e2e-spec.ts and fastify-adapter.e2e-spec.ts against a real sample Nest app in tests/src/.

Code Quality Unlike some smaller official Nest modules, this repo carries genuine end-to-end tests: a sample controller/module/main.ts under tests/src/ is booted with supertest against both the Express and Fastify adapters to verify static files are actually served and routes are excluded correctly, rather than relying on unit tests of mocked internals alone. Source files remain small and single-purpose, consistent with other official @nestjs/* modules.

API Design The module follows NestJS’s now-familiar forRoot()/forRootAsync() dynamic-module convention, so existing Nest developers can configure it with zero new concepts — pass a root path (default client) and optional exclusion patterns. Adapter detection is fully automatic, meaning the same configuration code works whether the app runs on Express or Fastify, which removes an entire class of adapter-specific configuration that would otherwise fall on the developer.

Used by 8 apps in this directory

TypeScript
96%
Other

Amplication

AI Code Assistants · Automation · Developer Tools

16,015

Create production-ready backend services with your organization's standards baked in — generating NestJS, Prisma, and GraphQL code that you own and control.

View details
58
Repo Health
84
Technical
60
Dependency
Built with
TypeScript 96%
Updated 3 months ago
TypeScript
97%
AGPL 3.0

Bigcapital

Invoicing Finance

3,916

Self-hostable double-entry accounting platform with invoicing, inventory, multi-currency, and real-time financial reporting for small and medium businesses.

View details
90
Repo Health
77
Technical
61
Dependency
Built with
TypeScript 97%
Updated 6 days ago
TypeScript
76%
AGPL 3.0

Ghostfolio

Invoicing Finance

9,367

Track your stocks, ETFs, and crypto with a privacy-first, self-hostable wealth management platform built for data-driven investors.

View details
93
Repo Health
79
Technical
67
Dependency
Built with
TypeScript 76%
HTML 21%
Updated 5 days ago
TypeScript
98%
Other

Hexabot

AI Development · Automation

1,260

Build and run agentic workflows across channels with YAML, tools, and RAG

View details
78
Repo Health
76
Technical
65
Dependency
Built with
TypeScript 98%
Updated 1 weeks ago
TypeScript
94%
AGPL 3.0

Laudspeaker

Automation · Marketing

2,626

Open-source customer engagement platform for building visual, event-triggered messaging journeys across email, SMS, push, in-app, and webhooks.

View details
51
Repo Health
66
Technical
62
Dependency
Built with
TypeScript 94%
Updated 2 months ago
TypeScript
98%
Other

Novu

Developer Tools

40,084

Open-source communication infrastructure that connects your products and AI agents to every channel your users live on — Inbox, Email, SMS, Push, Chat, and more.

View details
93
Repo Health
80
Technical
64
Dependency
Built with
TypeScript 98%
Updated 5 days ago
JavaScript
57%
AGPL 3.0

ToolJet

AI Agents · Low Code Platforms · No Code Platforms

41,007

Open-source AI-native platform to build and deploy internal tools, workflows, and AI agents with a visual drag-and-drop builder and 80+ data source integrations.

View details
94
Repo Health
81
Technical
63
Dependency
Built with
JavaScript 57%
TypeScript 36%
Updated 4 days ago
TypeScript
82%
Other

twenty

CRM

57,585

The open-source CRM you build, ship, and version like the rest of your stack — with customizable objects, AI agents, and a TypeScript SDK.

View details
92
Repo Health
82
Technical
64
Dependency
Built with
TypeScript 82%
MDX 15%
Updated 4 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers