http-proxy-middleware
- Version 4.0.0
- Published
- 82.9 kB
- 5 dependencies
- MIT license
Install
npm i http-proxy-middlewareyarn add http-proxy-middlewarepnpm add http-proxy-middlewareOverview
The one-liner node.js proxy middleware for connect, express, next.js and more
Index
Variables
variable debugProxyErrorsPlugin
const debugProxyErrorsPlugin: Plugin<http.IncomingMessage, http.ServerResponse>;Subscribe to to prevent server from crashing. Errors are logged with library.
variable errorResponsePlugin
const errorResponsePlugin: Plugin<http.IncomingMessage, http.ServerResponse>;variable loggerPlugin
const loggerPlugin: Plugin<http.IncomingMessage, http.ServerResponse>;variable proxyEventsPlugin
const proxyEventsPlugin: Plugin<http.IncomingMessage, http.ServerResponse>;Implements option.on object to subscribe to
httpxyevents.Example 1
createProxyMiddleware({on: {error: (error, req, res, target) => {},proxyReq: (proxyReq, req, res, options) => {},proxyReqWs: (proxyReq, req, socket, options) => {},proxyRes: (proxyRes, req, res) => {},open: (proxySocket) => {},close: (proxyRes, proxySocket, proxyHead) => {},start: (req, res, target) => {},end: (req, res, proxyRes) => {},econnreset: (error, req, res, target) => {},}});
Functions
function createProxyMiddleware
createProxyMiddleware: < TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse, TNext = (err?: any) => void>( options: Options<TReq, TRes>) => RequestHandler<TReq, TRes, TNext>;Create proxy middleware for Express-like servers. ([list of servers with examples](https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md))
Example 1
Basic proxy to a single target.
import { createProxyMiddleware } from 'http-proxy-middleware';const proxy = createProxyMiddleware({target: 'http://www.example.org',changeOrigin: true,});Example 2
Proxy only matching paths and rewrite the forwarded path.
import { createProxyMiddleware } from 'http-proxy-middleware';const proxy = createProxyMiddleware({target: 'http://localhost:3000',pathFilter: '/api',pathRewrite: {'^/api/': '/',},});Example 3
Native path rewrite by mounting at a route (alternative to
pathRewrite).import express from 'express';import { createProxyMiddleware } from 'http-proxy-middleware';const app = express();app.use('/users',createProxyMiddleware({target: 'http://jsonplaceholder.typicode.com/users',changeOrigin: true,}),);Example 4
Use framework-specific request/response types (Express).
import type { Request, Response } from 'express';import { createProxyMiddleware } from 'http-proxy-middleware';const proxy = createProxyMiddleware<Request, Response>({target: 'http://www.example.org/api',changeOrigin: true,});Example 5
Intercept and modify a proxied response body.
import { createProxyMiddleware, responseInterceptor } from 'http-proxy-middleware';const proxy = createProxyMiddleware({target: 'http://www.example.org',selfHandleResponse: true,on: {proxyRes: responseInterceptor(async (responseBuffer) => {const response = responseBuffer.toString('utf8');return response.replace('Hello', 'Goodbye');}),},});See Also
https://github.com/chimurai/http-proxy-middleware/
https://github.com/chimurai/http-proxy-middleware/#basic-usage
https://github.com/chimurai/http-proxy-middleware/#intercept-and-manipulate-responses
https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md
https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md
https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md
https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/response-interceptor.md
function fixRequestBody
fixRequestBody: <TReq extends unknown = any>( proxyReq: http.ClientRequest, req: TReq) => void;Fix proxied body if bodyParser is involved.
function responseInterceptor
responseInterceptor: < TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse>( interceptor: Interceptor<TReq, TRes>) => (proxyRes: http.IncomingMessage, req: TReq, res: TRes) => Promise<void>;Intercept responses from upstream. Automatically decompress (deflate, gzip, brotli). Give developer the opportunity to modify intercepted Buffer and http.ServerResponse
NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
Interfaces
interface Options
interface Options< TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse> extends ProxyServerOptions {}property ejectPlugins
ejectPlugins?: boolean;Eject pre-configured plugins. NOTE: register your own error handlers to prevent server from crashing.
https://github.com/chimurai/http-proxy-middleware#ejectplugins-boolean-default-false v3.0.0
property logger
logger?: Logger;Log information from http-proxy-middleware
Example 1
createProxyMiddleware({logger: console});https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/logger.md v3.0.0
property on
on?: OnProxyEvent<TReq, TRes>;Listen to
httpxyeventsExample 1
createProxyMiddleware({on: {error: (error, req, res, target) => {console.error(error);}}});https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/proxy-events.md v3.0.0
See Also
OnProxyEvent for available events
property pathFilter
pathFilter?: Filter<TReq>;Narrow down requests to proxy or not. Filter on which is relative to the proxy's "mounting" point in the server. Or use the object for more complex filtering. https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md v3.0.0
property pathRewrite
pathRewrite?: PathRewriteConfig<TReq>;Modify request paths before requests are send to the target.
Example 1
createProxyMiddleware({pathRewrite: {'^/api/old-path': '/api/new-path', // rewrite path}});https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md
property plugins
plugins?: Plugin<TReq, TRes>[];Access the internal
httpxyserver instance to customize behaviorExample 1
createProxyMiddleware({plugins: [(proxyServer, options) => {proxyServer.on('error', (error, req, res) => {console.error(error);});}]});https://github.com/chimurai/http-proxy-middleware#plugins-array v3.0.0
property router
router?: | Record<string, ProxyServerOptions['target']> | ((req: TReq) => ProxyServerOptions['target']) | ((req: TReq) => Promise<ProxyServerOptions['target']>);Dynamically set the .
Example 1
createProxyMiddleware({router: async (req) => {return 'http://127:0.0.1:3000';}});https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/router.md
interface Plugin
interface Plugin< TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse> {}call signature
(proxyServer: ProxyServer<TReq, TRes>, options: Options<TReq, TRes>): void;interface RequestHandler
interface RequestHandler< TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse, TNext = NextFunction> {}property upgrade
upgrade: (req: TReq, socket: net.Socket, head: Buffer) => void;call signature
(req: TReq, res: TRes, next?: TNext): Promise<void>;Type Aliases
type Filter
type Filter<TReq extends http.IncomingMessage = http.IncomingMessage> = | string | string[] | ((pathname: string, req: TReq) => boolean);Package Files (9)
Dependencies (5)
Dev Dependencies (32)
- @commitlint/cli
- @commitlint/config-conventional
- @eslint/js
- @hono/node-server
- @types/debug
- @types/eslint
- @types/express
- @types/is-glob
- @types/micromatch
- @types/node
- @types/supertest
- @types/ws
- @vitest/coverage-v8
- body-parser
- eslint
- express
- get-port
- globals
- hono
- husky
- lint-staged
- mockttp
- msw
- nock
- open
- oxfmt
- pkg-pr-new
- supertest
- typescript
- typescript-eslint
- vitest
- ws
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/http-proxy-middleware.
- Markdown[](https://www.jsdocs.io/package/http-proxy-middleware)
- HTML<a href="https://www.jsdocs.io/package/http-proxy-middleware"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 2583 ms. - Missing or incorrect documentation? Open an issue for this package.
