0

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

  • Have you imported the NestMiddleWare in your application? You can refer the following link if it helps: https://github.com/lujakob/nestjs-realworld-example-app/blob/master/src/user/auth.middleware.ts – Sunny Parekh Jun 16 '23 at 05:54
  • Yes i had import package of middleware `import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { Transport, MicroserviceOptions } from '@nestjs/microservices' import { protobufPackage } from './auth/auth.pb'; const Fingerprint = require('express-fingerprint');` – phamphuong Jun 16 '23 at 08:21

2 Answers2

0

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.

Sunny Parekh
  • 945
  • 7
  • 17
-1

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.

berat
  • 63
  • 5
  • Many of your answers appear likely to have been entirely or partially written by AI (e.g., ChatGPT). As a heads-up, [posting of AI-generated content is not permitted on Stack Overflow](//meta.stackoverflow.com/q/421831). If you used an AI tool for assistance on this answer, could I ask you to (1) Reply in a comment here confirming that it was AI-generated, along with what tool (e.g., ChatGPT, Bing Chat, Copilot, etc.) -- We're compiling data on AI-assisted answers and could use your help. (2) After commenting, I recommend deleting your answer. Thanks! – NotTheDr01ds Jun 23 '23 at 12:06
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 23 '23 at 12:06