Skip to content

NestJS

You should build your application as a tree of modules, controllers, and providers. The seed app exposes a single GET / endpoint that returns "Hello, World!" — extend it by adding more routes, services, and modules following the same pattern.

Entry file

The solution files are organized under src/:

  • src/main.ts — bootstraps the application and starts the HTTP server on port 3000 (preloaded, hidden, read-only).
  • src/app.module.ts — the root module wiring controllers and providers (preloaded, read-only).
  • src/app.controller.ts — the root controller exposing GET / (preloaded, read-only).
  • src/app.service.ts — the service providing the response string (editable solution file).

Server configuration for API Tester

The server is started by src/main.ts (NestFactory.create(AppModule) listening on port 3000) so it is running when you use the API Tester tab. The API Tester sends real HTTP requests against your NestJS app and shows the response side-by-side.

typescript
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap(): Promise<void> {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}

bootstrap();

Version

Running on Node.js v22.0.0 with NestJS 11.

Supported languages

TypeScript

Testing framework

Vitest

Special reminders and implementation details

  • Every provider needs the @Injectable() decorator. Forgetting it leads to a confusing Nest can't resolve dependencies error at runtime.
  • Every controller and provider must be registered in a module's controllers: [...] or providers: [...] array. If you add FooService but forget to list it, NestJS won't see it.
  • Tests use @nestjs/testing to spin up a small TestingModule with just the providers a controller needs — no need to bootstrap the full app.

Example with @nestjs/testing:

typescript
import 'reflect-metadata';
import { describe, it, expect, beforeEach } from 'vitest';
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './src/app.controller';
import { AppService } from './src/app.service';

describe('AppController', () => {
  let controller: AppController;

  beforeEach(async () => {
    const moduleRef: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [AppService]
    }).compile();

    controller = moduleRef.get<AppController>(AppController);
  });

  it('returns "Hello, World!" on GET /', () => {
    expect(controller.getHello()).toBe('Hello, World!');
  });
});

Included libraries

How to debug

NestJS is a backend framework — your code runs in Node, not the browser. Three main ways to inspect runtime behavior:

1. Logs from controllers and services

Use console.log or the built-in Logger for structured output:

typescript
import { Controller, Get, Logger } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  private readonly logger = new Logger(ItemsController.name);

  @Get()
  list() {
    this.logger.log('GET /items called');
    return [];
  }
}

Output appears in the dev server terminal (or in the embedded Preview's debug panel).

2. API Tester

Use the API Tester tab in the right panel to send real HTTP requests (GET / POST / PUT / PATCH / DELETE) to your running NestJS server. Pick a method, type a path (e.g. /items), add headers/params/body, and hit Send. The response (status, headers, body, time) renders side-by-side so you can iterate on controllers and services without leaving the editor.

3. Steps to Debug Using Console Logs

  1. Identify the Problem Area: Decide whether the issue is in the controller, a service, or a custom provider.

  2. Insert console.log() Statements: Add log calls before and after suspicious code. All NestJS logs go to the server terminal.

  3. Check the Results Output of your tests

  4. Use Debug Icon in Preview Area: Click on the debug icon in the Preview area to check the terminal output.

  5. Analyze the Output: Compare logs against the API Tester responses to spot wrong values, missing providers, or unhandled errors.

Common pitfalls

  • Missing @Injectable() on a provider → DI fails with a confusing "Nest can't resolve dependencies" error. Add the decorator to every service.
  • Forgot to register in app.module.ts → controller or provider is invisible to the runtime. Add it to controllers: [...] or providers: [...].
  • Test failures with Nest can't resolve dependencies in Test.createTestingModule(...) → list every provider the controller needs, even transitively.