API Reference

http-proxy

Low-level HTTP proxying library that forwards HTTP and WebSocket requests to target servers with event hooks.

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

Installation

npmnpm install http-proxy
pnpmpnpm add http-proxy

Methods

Overview

http-proxy is a low-level HTTP proxy library that forwards HTTP and WebSocket requests to target servers with event hooks. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • createProxyServer — Creates a proxy server instance with a default target and behavior options.
  • proxy.web — Forwards an HTTP request and response pair to the target.
  • proxy.ws — Forwards a raw WebSocket upgrade request to the target.

Example

const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({ target: 'http://localhost:9000' });
proxy.on('error', (err, req, res) => { res.writeHead(502).end(String(err)); });

http.createServer((req, res) => proxy.web(req, res)).listen(8000);

Notes

Always handle the error event, otherwise failures leak stack traces to clients. changeOrigin controls whether the Host header is rewritten.

Verification

Proxy a request to a target and confirm the response body arrives, then stop the target and confirm the error handler responds with a controlled status.

FAQ

Why do I get an ugly stack trace when the target is down?
Listen for the proxy error event and send a controlled response; otherwise the default handler writes an error page.
How do I preserve the original Host header?
Set changeOrigin to false so the target receives the original host, or true to rewrite it to the target host.
Can it proxy WebSockets?
Yes. Handle the server upgrade event and call proxy.ws with the socket and head arguments.