Tag
nestjs
Published articles tagged nestjs.
Application lifecycle hooks in NestJS
NestJS calls methods on your providers at fixed points while the application starts and stops. This chapter lists the hooks and their order, shows where startup work and cleanup belong, and explains why shutdown hooks have to be switched on before a container stop is handled gracefully.
Authentication and authorization in GraphQL
GraphQL needs identity in context and authorization at field granularity. This chapter covers transport auth, resolver and field guards, NestJS GqlExecutionContext, node(id) IDOR risks, and subscription connection_init.
Authentication in NestJS
NestJS authentication is built from guards: one checks a password and issues a token, another verifies that token on every later request. This chapter wires Passport's local and JWT strategies into a Nest app, then protects every route by default with a global guard and a @Public() opt-out.
Authorization in NestJS
Once a NestJS request has a user, authorization decides what that user may do. This chapter builds role checks from a typed decorator and a guard, orders them after authentication, adds a @CurrentUser() parameter, and moves ownership checks into the service where the data is.
Building a GraphQL server with NestJS
NestJS can expose a code-first GraphQL API with the Apollo driver, auto- generated SDL, decorators for types and resolvers, validation pipes, request- scoped DataLoaders, and graphql-ws subscriptions. This runbook walks from install to a working endpoint.
Configuration in NestJS
@nestjs/config loads environment variables and .env files into an injectable ConfigService. This chapter covers loading and precedence, namespaced and typed configuration, failing fast on missing values, and giving each feature module only the settings it needs.
Controllers in NestJS
Controllers map HTTP routes to methods and turn their return values into responses. This chapter covers route decorators, reading parameters, query strings and bodies into DTOs, status codes and headers, and the escape hatch to the underlying Express response.
Dynamic modules in NestJS
forRoot(), forFeature(), and forRootAsync() are how NestJS modules accept options from the application that imports them. This chapter shows what a dynamic module returns, the naming conventions, async configuration through the injector, and generating the boilerplate with ConfigurableModuleBuilder.
Exception filters in NestJS
Exception filters are where a NestJS application decides what an error looks like to the client. This chapter covers the built-in HTTP exceptions, attaching a cause, custom exception classes, writing a filter, and a catch-all filter that gives every error one shape.
GraphQL federation
Federation lets independent teams publish subgraphs that compose into one supergraph queried through a router. This overview explains composition, entities and @key, migration directives, major platforms, a NestJS subgraph sketch, and when federation is worth the operational cost.
Guards in NestJS
A guard answers one question before a NestJS handler runs: may this request continue? This chapter covers the CanActivate contract, reading the request and the handler from the ExecutionContext, steering a guard with metadata, and binding guards to routes or to the whole application.
Interceptors in NestJS
An interceptor wraps a NestJS route handler, so it can act both before the handler runs and on whatever the handler returns. This chapter covers the NestInterceptor contract, the RxJS operators that do most of the work, and the built-in interceptors for serialization and caching.
Limiting query depth and cost
Depth limits, complexity analysis, alias caps, timeouts, persisted query allowlists, and disabling introspection protect GraphQL from expensive operations. This chapter shows NestJS complexity plugins and gateway-level controls.
Middleware in NestJS
Middleware is the first code a NestJS request meets, and the only stage that runs before Nest knows which handler will answer. This chapter covers class and function middleware, binding it to routes in a module, global middleware, and the jobs that belong in a later stage instead.
Modules in NestJS
A NestJS module is the unit that decides which classes exist, which ones other parts of the app may use, and what it depends on. This chapter covers the root and feature modules, exports and imports, shared and re-exported modules, and the narrow case for global modules.
MongoDB with Mongoose in NestJS
@nestjs/mongoose wraps Mongoose in a dynamic module, so a connection is configured once and models are injected like any provider. This chapter connects with settings from ConfigService, defines a schema with decorators, and builds a service around an injected model.
NestJS glossary
The terms used across the NestJS series, from modules and providers through the request lifecycle, configuration, security, and testing. Each entry is a short definition with a link to the chapter that covers it in depth.
Pipes and validation in NestJS
Pipes are the last stage before a NestJS handler runs, and they see each argument on its own. This chapter covers the built-in parsing pipes, request validation with ValidationPipe and class-validator, writing a custom pipe, and where pipes should be bound.
Providers and dependency injection in NestJS
Providers hold the work of a Nest application, and the injector decides when to create them and who receives them. This chapter shows how the injector resolves a dependency by token, the four kinds of custom provider, injection scopes, and how to break a circular dependency.
Resolvers and execution
Resolvers connect schema fields to data sources. This chapter explains the four resolver arguments, default resolvers, parallel query execution, per- request context, the N+1 problem, DataLoader, and how errors bubble through nullable fields.
Testing NestJS applications
Dependency injection is what makes a NestJS app easy to test: any provider can be swapped for a fake before the module is compiled. This chapter covers unit tests with the testing module, overriding providers and guards, and end-to-end tests that send real HTTP requests with supertest.
The NestJS request lifecycle
Between the socket and your route handler, a NestJS request passes through middleware, guards, interceptors, and pipes, and exception filters wait for anything that goes wrong. This overview shows the order, what each stage is for, and how global, controller, and route bindings stack.
What is NestJS
NestJS is a TypeScript framework that gives a Node.js server a fixed architecture: modules, controllers, and providers, wired together by dependency injection. This overview shows what it adds on top of Express or Fastify, how a project starts, and when the structure is worth it.