NestJS — Enterprise Node.js

Events, HTTP Module & Outbound Resilience

18 min Lesson 60 of 80

Events, HTTP Module & Outbound Resilience

Not every action should be tightly coupled to a request handler. Domain events decouple internal reactions, while HttpModule provides an injectable Axios client for outbound calls. Together they support clean integrations when paired with timeouts, retries, and idempotency.

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:

  • EventEmitterModule lets one event trigger multiple listeners without the publisher knowing them.
  • Events are useful for local side effects such as audit logs, email requests, and cache invalidation.
  • HttpModule.registerAsync() configures Axios clients from ConfigService or secret-backed settings.
  • Outbound calls need timeouts and controlled retries; never allow a downstream API to block a request indefinitely.
  • Use idempotency keys when retrying write operations against external services.

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:

@Injectable() export class OrdersService { constructor(private readonly events: EventEmitter2) {} async create(dto: CreateOrderDto) { const order = await this.repo.save(dto); this.events.emit('order.created', new OrderCreatedEvent(order.id)); return order; } } HttpModule.registerAsync({ inject: [ConfigService], useFactory: (config: ConfigService) => ({ baseURL: config.getOrThrow('PAYMENTS_URL'), timeout: 3000, }), });
Design note: Events are not a distributed message broker. They are excellent inside one process; use queues or broker-backed microservices when delivery must survive process crashes.

Production checklist

  • Name events with domain language, not implementation details.
  • Keep listeners idempotent when they may be retried manually.
  • Set outbound HTTP timeouts globally.
  • Wrap external writes with idempotency keys and clear error mapping.
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.