NestJS — Enterprise Node.js

Global Prefix, Raw Body & Hybrid Apps

18 min Lesson 56 of 80

Global Prefix, Raw Body & Hybrid Apps

Production APIs often need platform-level HTTP choices that affect every route: a global /api prefix, URI or header versioning, raw request bodies for webhook signatures, HTTPS options, or a hybrid HTTP plus microservice application.

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:

  • setGlobalPrefix() keeps API routes grouped and lets static or frontend routes live beside them.
  • enableVersioning() supports URI, header, media type, or custom version extraction strategies.
  • Raw body support must be enabled before body parsing when validating Stripe, GitHub, or payment-provider signatures.
  • connectMicroservice() attaches a microservice server to the same Nest application context.
  • HTTPS and multiple servers can be configured at bootstrap, but a reverse proxy is often cleaner in production.

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:

const app = await NestFactory.create(AppModule, { rawBody: true }); app.setGlobalPrefix('api', { exclude: ['health'] }); app.enableVersioning({ type: VersioningType.URI }); app.connectMicroservice<MicroserviceOptions>({ transport: Transport.TCP, options: { host: '0.0.0.0', port: 4001 }, }); await app.startAllMicroservices(); await app.listen(3000);
Design note: These choices belong in bootstrap code because they define the shape of the whole application. Changing them later can break clients, webhooks, and infrastructure routing.

Production checklist

  • Document global prefixes and versioning in OpenAPI.
  • Exclude health checks from auth and global prefixes only when infrastructure requires it.
  • Test raw body signatures before enabling global validation or body transforms.
  • Keep hybrid app lifecycle in one bootstrap function so shutdown behavior is predictable.
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.