@nestjs-kitchen/proxy
v1.0.1
Published
Provides an HTTP proxy functionality in the NestJS style.
Maintainers
Readme
@nestjs-kitchen/proxy
Provides an HTTP proxy functionality in the NestJS style.
Feature
✅ Provides a NestJS-style API.
✅ Built on top of
http-proxy-middleware✅ Compatible with both
@nestjs/platform-expressand@nestjs/platform-fastify.
Install
$ npm install --save @nestjs-kitchen/proxyUsage
Note:
rawBodyoption should be enabled first. SeeRaw body.
// express
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
rawBody: true,
});or
// fastify
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
{
rawBody: true,
},
);Example
Register module:
// in app.module.ts
@Module({
imports: [
// ...
ProxyModule.register({
options: {
target: 'https://you.com/target/api',
changeOrigin: true,
agent: new Agent({ keepAlive: true })
}
})
// ...
],
controllers: [AppController],
providers: [],
})
export class AppModule {}Apply Proxy in a Controller:
// in app.controller.ts
import { Controller, All, Post } from '@nestjs/common';
import { ProxyInterceptor } from '@nestjs-kitchen/proxy';
@UseInterceptors(ProxyInterceptor)
@Controller()
export class AppController {
@ProxyInterceptor.Use({
headers: { 'x-handler': 'ok' },
pathRewrite: { '^/proxy-api': '' }
})
@All('/proxy-api/*')
proxyApi() {
return true;
}
}Options
See http-proxy-middleware#options.
License
MIT License
