Home Projects Portfolio Dashboard Export PDF Log in

Building a Robust NestJS Foundation: Initial Setup and Health Checks

We recently kicked off development for the Vive Tu Mente project, focusing on creating a scalable and maintainable backend. Our choice for the backend framework was NestJS, known for its modular architecture and strong support for TypeScript. This post details the crucial initial steps taken to establish a solid foundation, including essential configurations and a vital health check endpoint.

The Problem

Starting any new backend project requires careful foundational setup to ensure smooth development and reliable deployment. Without proper configuration, issues like cross-origin resource sharing (CORS) can block local frontend development, inconsistent API paths can complicate integration, and a lack of basic service monitoring can hinder operational visibility. We needed a systematic way to handle environment-specific variables, define a clear API entry point, and verify service liveness from day one.

The Solution: NestJS Initial Configuration

NestJS provided the perfect structure to address these challenges.

  • Environment Variables: We integrated a robust way to manage environment variables, ensuring sensitive data and configuration settings are externalized and easily managed across different environments.
  • Global API Prefix: To maintain consistent API routing, a global prefix was applied to all routes, making it clear that all backend requests fall under a specific namespace (e.g., /api).
  • CORS Configuration: For local development, it's critical to allow the frontend application to communicate with the backend. We configured CORS to permit requests from our local development frontend server.
  • Health Check Endpoint: A fundamental requirement for any microservice or backend application is a health check. This simple endpoint allows monitoring systems to verify that the service is running and responsive.

Here's an illustrative example of the main.ts setup and a simple health check controller:

// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Apply global API prefix
  app.setGlobalPrefix('api');

  // Configure CORS for local development
  app.enableCors({
    origin: 'http://localhost:3000', // Frontend origin
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    credentials: true,
  });

  // Listen on a specified port, potentially from environment variables
  const port = process.env.PORT || 3001;
  await app.listen(port);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
// src/health/health.controller.ts
import { Controller, Get } from '@nestjs/common';

@Controller('health')
export class HealthController {
  @Get()
  check() {
    return { status: 'ok', uptime: process.uptime() };
  }
}

The main.ts sets up our core application with necessary middleware, while the HealthController provides a simple /api/health endpoint to confirm the service's operational status.

Benefits of This Setup

While not "results after six months" in the traditional sense, this foundational work immediately provides immense benefits:

  • Streamlined Development: Frontend and backend teams can integrate seamlessly from the start without CORS headaches.
  • Consistent API Surface: The global prefix ensures all API endpoints follow a predictable pattern.
  • Operational Visibility: The health check endpoint offers immediate feedback on service availability, crucial for deployment and monitoring systems.
  • Future Scalability: A well-structured NestJS application, combined with externalized configuration, is primed for future growth and deployment across various environments.

Getting Started

  1. Initialize a new NestJS project (nest new project-name).
  2. Integrate @nestjs/config for environment variable management.
  3. Apply app.setGlobalPrefix() in your main.ts.
  4. Configure app.enableCors() with appropriate origins.
  5. Create a simple HealthController to expose a liveness check.
  6. Ensure your main.ts listens on a configurable port.

Key Insight

A strong foundation is paramount for any software project. Investing time in robust initial configurations, like environment variable handling, API prefixes, CORS, and health checks, pays dividends in developer experience, deployment reliability, and long-term maintainability. Don't skip the essentials; they are the bedrock of a successful application.


Generated with Gitvlg.com

Building a Robust NestJS Foundation: Initial Setup and Health Checks
SOFIA DESIREE BARTOLI

SOFIA DESIREE BARTOLI

Author

Share: