NestJS — Enterprise Node.js

MVC, Serve Static & Router Module

18 min Lesson 62 of 80

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:

@Module({ imports: [ ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'public'), serveRoot: '/assets', }), RouterModule.register([ { path: 'admin', module: AdminModule }, { path: 'api/v1', module: ApiModule }, ]), ], }) export class AppModule {}
Design note: These features make Nest viable for internal full-stack surfaces, but keep concerns clear: API modules, rendered pages, and static assets should not become one tangled module.

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.
Rule of thumb: If the feature makes boundaries clearer and tests easier, it is probably the right choice. If it hides dependencies or makes tracing harder, redesign.

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.