How To Keep Track Of Responses In NestJS

Benjamin Raffetseder - Aug 25 - - Dev Community

In backend development, effective logging is essential for maintaining a healthy and efficient system. It provides insights into the behavior of your application, aiding in debugging, troubleshooting, and performance optimization. NestJS offers a way to implement custom middleware, including logging mechanisms.

Introducing the Response Middleware

In this blog post, we will explore how to create a response logging middleware in NestJS. This middleware will log the incoming request, the response status, and the response time. By implementing this middleware, you can keep track of the requests and responses in your application, which is essential for monitoring and debugging.

Here's an example of a response logging middleware in NestJS:

import { Injectable, Logger, NestMiddleware } from '@nestjs/common'
import type { NextFunction, Request, Response } from 'express'

@Injectable()
export class ResponseMiddleware implements NestMiddleware {
  logger = new Logger('Response')

  constructor() {}

  /**
   * This middleware logs the request and response status code in the console.
   * The format of the log is:
   * <METHOD> <URL> <STATUS CODE> <RESPONSE TIME IN MS>
   *
   * @param {Request} req - The incoming request
   * @param {Response} res - The outgoing response
   * @param {NextFunction} next - The next middleware in the stack
   */
  use(req: Request, res: Response, next: NextFunction) {
    const { method, originalUrl } = req
    const requestStartTime = new Date().getTime()

    /**
     * This event is emitted when the response is finished.
     * We log the request and response information in this listener.
     */
    res.on('finish', () => {
      const { statusCode } = res

      const responseTime = new Date().getTime()
      const duration = responseTime - requestStartTime
      const message = `${method} ${originalUrl} ${statusCode} ${duration}ms`

      /**
       * Log the request and response information in the console.
       */
      this.logger.log(message)
    })

    /**
     * Call the next middleware in the stack.
     */
    next()
  }
}
Enter fullscreen mode Exit fullscreen mode

How It Works

The ResponseMiddleware class implements the NestMiddleware interface, which requires the implementation of a use method. This method is called for each incoming request, and it takes three parameters: req (the incoming request), res (the outgoing response), and next (the next middleware in the stack).

In the use method, we extract the HTTP method and the URL from the incoming request. We also record the start time of the request using new Date().getTime(). We then attach an event listener to the finish event of the response object. This event is emitted when the response is finished and ready to be sent back to the client.

When the finish event is triggered, we calculate the response time by subtracting the start time from the current time. We then construct a log message containing the HTTP method, URL, status code, and response time. Finally, we log this message using the Logger class provided by NestJS.

Key Features

  1. Enhanced Monitoring: By logging detailed information about each request and response, you can monitor your application's performance and identify slow endpoints.
  2. Simplified Debugging: The logs provide valuable insights into the behavior of your application, making it easier to debug issues and understand the flow of requests.
  3. Performance Optimization: By analyzing the response times, you can identify bottlenecks and optimize the performance of your application.

Registering the Middleware

To use this middleware in your NestJS application, you need to register it in the appropriate module. Here's how you can register the ResponseMiddleware:

import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import { ResponseMiddleware } from './response.middleware'

@Module({})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(ResponseMiddleware).forRoutes('*') // Apply the middleware to all routes
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In conclusion, implementing a response logging middleware in NestJS is a simple yet powerful way to enhance the monitoring and debugging capabilities of your backend.

You could extend this middleware to log additional information such as request headers, response headers, and request body or even integrate it with 3rd-party logging services.

. . .
Terabox Video Player