API Reference

http-proxy-middleware

Express-compatible middleware that wires http-proxy with path filtering, event hooks, and response rewriting.

Official Documentation ↗
CompatibilityThis documentation covers version v4.2.1. Check the official docs for the latest API surface.

Installation

npmnpm install http-proxy-middleware
pnpmpnpm add http-proxy-middleware

Methods

Overview

http-proxy-middleware is Express-compatible middleware that wires http-proxy with path filtering, lifecycle hooks, and response rewriting. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • createProxyMiddleware — Builds an Express-compatible middleware that forwards matching requests to a target.
  • pathFilter — Restricts which request paths the middleware handles.
  • on — Event hooks that run at points in the request lifecycle for rewriting or logging.

Example

const { createProxyMiddleware } = require('http-proxy-middleware');

app.use('/api', createProxyMiddleware({
  target: 'http://api.internal:8080',
  changeOrigin: true,
}));

Notes

Order matters: if a body parser consumes the request first, proxied POST bodies can be empty. Register the proxy before such middleware.

Verification

Mount the middleware on a prefix and confirm a request through it reaches the target and returns the expected body.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/chimurai/http-proxy-middleware@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:16.681Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

How do I proxy WebSockets?
Set ws: true in the options and ensure pathFilter matches the upgrade path so the middleware handles upgrades.
Why is my body empty on forwarded POST requests?
Another body parser may consume the stream first; register the proxy middleware before body parsers or fix the request body hook.
How do I rewrite paths?
Use pathRewrite, for example { "^/api": "/v1" }, to map the incoming prefix to the target path.