Comments (5)
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.
got the same issue
from http-proxy-middleware.
Is anyone working on that?
from http-proxy-middleware.
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.
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)
- Is there a program to support service discovery? For example consul, nacos HOT 1
- Proxy created with websocket stops working after a few minutes
- Cross origin issue all of a sudden
- POST request looses body HOT 2
- Unable to output console.log and my plugin is not running HOT 4
- v3 Next.js API Route Websocket multiple requests HOT 3
- Shadow/mirror requests HOT 2
- Express multipart/form-data multer files HOT 1
- Memory leak in function `responseInterceptor` of file `handlers/response-interceptor.ts` HOT 1
- Next.js App Router can't handle proxied requests HOT 17
- Filter example in README wrong. HOT 1
- Cannot override options in the `onProxyReq` method. HOT 1
- websocket messages interception & manipulation
- Intercept HTTP(s) calls and modify request "paths" on the fly HOT 1
- Issue obtaining body and updating it as part of proxy HOT 3
- Proxy request fails due to bodyParser HOT 1
- Fails to proxy from localhost to localhost HOT 2
- Too many subscriptions to Server.close event cause a OOM? HOT 4
- TS type compatibility HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http-proxy-middleware.