How to use middleware with Microservice in Nestjs?

525 views Asked by At

I'm trying to use middleware 'express-fingerprint' in my microservice application ? But i getting an error "Property 'use' does not exist on type 'INestMicroservice'.ts(2339)" ? How do i solve this? Here my code :

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule,{
    transport : Transport.GRPC,
    options : {
      url : '0.0.0.0:6000',
      package : protobufPackage,
      protoPath: 'node_modules/ecommerce-proto/proto/auth.proto'
    }
  } );
  app.use(
    Fingerprint({
      parameters: [
        Fingerprint.useragent,
        Fingerprint.acceptHeaders,
        Fingerprint.geoip,
      ],
    }),
  );
  await app.listen();
}
bootstrap();

Thanks all very much

2

There are 2 answers

0
Sunny Parekh On

To use middleware in your Nest.js microservice application, you can use NestFactory.create instead of NestFactory.createMicroservice.

Also, use app.useGlobalInterceptors instead of app.use to apply the FingerprintInterceptor to all incoming requests.

 app.useGlobalInterceptors(new FingerprintInterceptor());

At last just ensure you have @nestjs/platform-express installed in order to use NestFactory.create method.

2
berat On

It seems like you're trying to use express middleware in a NestJS microservice application, specifically the express-fingerprint middleware. Unfortunately, you're encountering a challenge because the NestJS microservice application doesn't expose the underlying express instance to directly use the middleware as you're doing.

For the use() method to work, you need to be dealing with an instance of an express-based server, which is not the case when using a microservice in NestJS. When using the microservice method, the NestFactory returns an instance of a microservice, not an HTTP-based server.

However, you can use middleware in a microservice setup by creating hybrid applications which allow you to utilize both HTTP server features (like middleware) and microservice transport layer features. Here's a sample code for such a hybrid application setup:

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as express from 'express';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import * as Fingerprint from 'express-fingerprint';

async function bootstrap() {
  const server = express();
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(server),
  );

  app.use(
    Fingerprint({
      parameters: [
        Fingerprint.useragent,
        Fingerprint.acceptHeaders,
        Fingerprint.geoip,
      ],
    }),
  );

  app.connectMicroservice<MicroserviceOptions>({
    transport: Transport.GRPC,
    options: {
      url: '0.0.0.0:6000',
      package: 'protobufPackage',
      protoPath: 'node_modules/ecommerce-proto/proto/auth.proto',
    },
  });

  await app.startAllMicroservicesAsync();
  await app.listen(3000);
}

bootstrap();

In this code, we first initialize the express server, then create a Nest application using the ExpressAdapter, and lastly, we connect the gRPC microservice to the Nest application.

Hope this helps, and feel free to ask if you have any further questions.

Note: Remember to replace 'protobufPackage' with your actual protobuf package name.

For more about hybrid applications in NestJS, you can check out the official NestJS documentation https://nestjs.com.