http-proxy-middleware
Express-compatible middleware that wires http-proxy with path filtering, event hooks, and response rewriting.
Official Documentation ↗v4.2.1. Check the official docs for the latest API surface.Installation
npm install http-proxy-middlewarepnpm add http-proxy-middlewareMethods
No methods match this filter.
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.
- NOTE: register your own error handlers to prevent server from crashing. (https://cdn.jsdelivr.net/gh/chimurai/http-proxy-middleware@latest/README.md)
- Only info, warn, error are used internally for compatibility across different loggers. (https://cdn.jsdelivr.net/gh/chimurai/http-proxy-middleware@latest/README.md)
- If your target server only accepts IPv4 connections, trying to proxy to localhost will fail if resolved to ::1 (IPv6). (https://cdn.jsdelivr.net/gh/chimurai/http-proxy-middleware@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.