MVC, Serve Static & Router Module
MVC, Serve Static & Router Module
NestJS can serve more than JSON APIs. It can render server-side views, expose public static assets, and organize complex route trees with RouterModule. These features are useful for admin panels, documentation portals, and small full-stack apps.
Core idea
This feature is about controlling how the application is organized and how it behaves at runtime. These are the points a developer should understand before using it in a real project:
- MVC mode configures a views directory and template engine, then returns rendered templates from controllers.
- ServeStaticModule exposes built frontend assets or public files without writing custom controllers.
- RouterModule.register() groups modules under route prefixes without hard-coding prefixes in every controller.
- Static assets should usually be cached aggressively, while rendered pages need application-specific cache rules.
- For large SPAs, a CDN or frontend hosting platform may be better than serving all assets through Nest.
Practical example
The following example shows the idea in a practical NestJS project. The goal is not to memorize the snippet, but to understand where it belongs in the architecture:
Production checklist
- Keep rendered controllers separate from API controllers.
- Serve hashed static assets with long cache lifetimes.
- Use RouterModule for large route trees and bounded contexts.
- Move heavy public assets to CDN when traffic grows.
Summary
This lesson covers an advanced NestJS area that matters when building enterprise applications. Focus on clear boundaries, testable behavior, and choosing the right tool for the context instead of using every feature everywhere.