NestJS Basics
What is NestJS and why would you choose it over plain Express?
NestJS is a TypeScript framework for building server-side Node.js applications. It runs on Express (or Fastify) and adds an opinionated architecture: modules, controllers, providers and a built-in dependency injection container.
Choose it for medium/large apps and teams: consistent structure, DI makes testing easy, and validation, guards, config, OpenAPI, queues, WebSockets and microservices are first-class. Plain Express is fine for very small services where the extra concepts are not worth it.
What are the three main building blocks of a NestJS app?
- Modules (
@Module): group related code and define what is visible to other modules. - Controllers (
@Controller): handle incoming requests and return responses. - Providers (
@Injectable): services, repositories, factories, injected by the DI container.
The root AppModule is passed to NestFactory.create() in main.ts, and Nest builds the whole dependency graph from it.
What does NestFactory.create() do?
It creates the application: scans the module tree from the root module, instantiates all providers and controllers in dependency order, maps routes, and returns an INestApplication. On it you configure global things (useGlobalPipes, enableCors, setGlobalPrefix, enableShutdownHooks) and call listen(port).
How does NestJS know what to inject into a constructor?
TypeScript's emitDecoratorMetadata writes the constructor parameter types as design:paramtypes metadata on decorated classes. Nest reads it with reflect-metadata and looks up a provider for each type (token) in the module's scope.
That is why you inject classes, not interfaces (interfaces do not exist at runtime). For interfaces, strings or symbols, use @Inject(TOKEN).
What is the default HTTP status code of a NestJS handler, and how do you change it?
200 for all methods except POST, which returns 201. Change it with @HttpCode(204), or throw an HttpException subclass for errors. For full control you can inject the response with @Res({ passthrough: true }) and call res.status().