Giter VIP home page Giter VIP logo

Comments (5)

chimurai avatar chimurai commented on June 13, 2024 1

Try to create the proxy once, instead of on every request.

Similar issue: #108 (comment)

Could you share what server are you using?

from http-proxy-middleware.

taozhi8833998 avatar taozhi8833998 commented on June 13, 2024

got the same issue

from http-proxy-middleware.

RadekKpc avatar RadekKpc commented on June 13, 2024

Is anyone working on that?

from http-proxy-middleware.

tak1n avatar tak1n commented on June 13, 2024

We are facing the same issue on a NestJS Application trying to leverage http-proxy-middleware through a Nest Middleware.

// proxy.middleware.ts
import {
  Inject,
  NestMiddleware,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { ConfigurationService } from '../configuration/configuration.service';
import { randomUUID } from 'node:crypto';

export class ProxyMiddleware implements NestMiddleware {
  constructor(
    private configService: ConfigurationService,
  ) {}

  async use(req: Request, res: Response, next: () => void): Promise<any> {
    const upstreamInfo = <method to get jwt etc>;
    const correlationId = randomUUID();

    const proxy = createProxyMiddleware({
      target: this.configService.getProxyTarget(),
      headers: {
        host: this.configService.getUpstreamHost(),
        authorization: `Bearer ${upstreamInfo.jwt}`,
        'x-organization-id': upstreamInfo.organizationId,
        'x-correlation-id': correlationId,
      },
      changeOrigin: false,
      xfwd: true,
    });

    proxy(req, res, next);
  }
}

// proxy.module.ts
import {
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from '@nestjs/common';
import { ConfigurationModule } from '../configuration/configuration.module';
import { ProxyMiddleware } from './proxy.middleware';

@Module({
  providers: [],
  imports: [
    ConfigurationModule,
  ],
})
export class ProxyModule implements NestModule {
  configure(consumer: MiddlewareConsumer): any {
    consumer
      .apply(ProxyMiddleware)
      .exclude(
        { path: 'ready', method: RequestMethod.GET },
        { path: 'live', method: RequestMethod.GET },
      )
      .forRoutes('');
  }
}

Try to create the proxy once, instead of on every request.

Is there a way to create the proxy instance only once but still be able to dynamically set headers for the proxied request? I know of onProxyReq to manipulate requests, but having a hard time figuring out a way to use that with a Nest Middleware that uses dependencies for various required functionalities (issue JWT, etc.).

from http-proxy-middleware.

tak1n avatar tak1n commented on June 13, 2024

Leaving this here for anyone facing the same issue with a NestJS application. The solution was like suggested by @chimurai to only call createProxyMiddleware once.

// proxy.middleware.ts
import {
  Inject,
  NestMiddleware,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { RequestHandler, createProxyMiddleware } from 'http-proxy-middleware';
import { ConfigurationService } from '../configuration/configuration.service';
import { randomUUID } from 'node:crypto';

export class ProxyMiddleware implements NestMiddleware {
  private proxy: RequestHandler;

  constructor(
    private configService: ConfigurationService,
  ) {
    this.proxy = createProxyMiddleware({
      target: this.configService.getProxyTarget(),
      headers: {
        host: this.configService.getUpstreamHost(),
      },
      changeOrigin: false,
      xfwd: true,
    });
  }

  async use(req: Request, res: Response, next: () => void): Promise<any> {
    const upstreamInfo = <method to get jwt etc>;
    const correlationId = randomUUID();

    req.headers = {
      ...req.headers,
      authorization: `Bearer ${upstreamInfo.jwt}`,
      'x-organization-id': upstreamInfo.organizationId,
      'x-correlation-id': correlationId,
    }

    this.proxy(req, res, next);
  }
}

from http-proxy-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.