Sling Academy
Home/Node.js/How to Compress Response Payload in NestJS

How to Compress Response Payload in NestJS

Last updated: January 01, 2024

Introduction

Optimizing application performance is crucial, and one aspect of this is minimizing the size of the response payload. In NestJS, several techniques can aid in compressing your responses, thus enhancing overall efficiency. This guide dives into various methods to achieve payload compression in NestJS using the latest TypeScript syntax.

Getting Started with Compression

Let’s begin with the simplest approach to compress response payloads in a NestJS application. The easiest way is to use a dedicated middleware like compression.

1 . First, install the compression middleware:

npm install compression

2. Next, integrate the compression middleware into your NestJS application. Edit the main.ts file to import and use it as follows:

import * as compression from 'compression';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  // Use compression middleware
  app.use(compression());

  // Start listening on port 3000
  await app.listen(3000);
}

bootstrap();

In this code:

  • The compression middleware is imported and applied to the Nest.js application. This middleware will compress response bodies for all requests, which is a good practice for improving the performance of web applications.
  • The application listens on port 3000, which is a common default for local development.

This basic setup will compress all outgoing responses that benefit from compression.

Custom Compression Options

For more control, you can use the options parameter to customize the behavior of the compression middleware:

app.use(compression({
  threshold: 1024, // Only compress responses that are larger than 1 KB
  level: 6 // Compression level from 1 (fastest) to 9 (most efficient)
}));

This allows you to set a compression level and threshold based on your application’s needs.

Advanced Compression Strategies

Some use cases require different types or levels of compression based on the request or response type. You can achieve this by creating custom middleware in NestJS:

import { Injectable, NestMiddleware } from '@nestjs/common';
import * as compression from 'compression';

@Injectable()
export class CustomCompressionMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: () => void) {
    if (req.headers['x-custom-compress']) {
      compression({ level: 9 })(req, res, next);
    } else {
      next();
    }
  }
}

Apply the CustomCompressionMiddleware within your module using the configure method:

import { Module, MiddlewareConsumer } from '@nestjs/common';

@Module({/* ... */})
export class ApplicationModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(CustomCompressionMiddleware)
      .forRoutes('*'); // Apply to all routes
  }
}

In this example, compression is enabled conditionally, based on the presence of a specific request header.

Conditional Compression with Interceptors

Interceptors offer a powerful way to manage response compression conditionally. Below is an example of an interceptor that compresses JSON responses:

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { map } from 'rxjs/operators';
import * as zlib from 'zlib';

@Injectable()
export class CompressionInterceptor implements NestInterceptor {
  async intercept(context: ExecutionContext, next: CallHandler) {
    return next
      .handle()
      .pipe(
        map(data => {
          const buffer = Buffer.from(JSON.stringify(data));
          return zlib.gzipSync(buffer);
        })
      );
  }
}

Apply the interceptor globally, or to specific routes or controllers as needed:

import { APP_INTERCEPTOR } from '@nestjs/core';

@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: CompressionInterceptor,
    },
  ],
})
export class ApplicationModule {}

With the interceptor applied, ensure you set the correct ‘Content-Encoding’ in your response headers.

Content Encoding Based on Client Support

A flexible approach might involve varying the compression algorithm based on client support (e.g., gzip, deflate, br). This could be intelligently handled by an intermediary piece of software or a more sophisticated middleware.

Conclusion

Compressing response payloads in NestJS is straightforward with middleware like compression and more complex with interceptors that offer granular control. Apply these techniques wisely, taking into account the nature of your data, network conditions, and client capabilities. This not only optimizes performance but also enhances user experience by ensuring fast data delivery where it matters most.

Next Article: How to Manage Events in NestJS

Previous Article: NestJS UnhandledPromiseRejectionWarning: How to Solve It

Series: Nest.js Tutorials: From Basics to Advanced

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates