Node.js HTTP Proxy (Node 16+ fork)
This package, `http-proxy-node16`, currently at version 1.0.6, is a specialized fork of the popular `node-http-proxy` library. Its primary purpose is to ensure robust compatibility and functionality within Node.js environments version 16.x and later, explicitly addressing potential breaking changes, deprecations, or compatibility issues that might arise when using the original `http-proxy` on newer Node.js runtimes. It offers comprehensive programmable HTTP and WebSocket proxying capabilities, making it an ideal choice for implementing components such as reverse proxies, sophisticated load balancers, or custom HTTP proxy middleware. A key differentiator from the upstream `node-http-proxy` is its explicit focus and testing against Node.js v16 and beyond, providing a reliable solution for applications deployed in these modern environments. The project indicates a maintenance approach welcoming "bug-fix PRs," suggesting a reactive, community-driven cadence for updates. This fork serves as a targeted solution for developers seeking a stable proxying library specifically for their Node.js 16+ applications, where the original project might have broader compatibility concerns or different priorities.
Common errors
-
connect ECONNREFUSED 127.0.0.1:9000
cause The target server configured for the proxy (e.g., `http://localhost:9000`) is not running or not accessible.fixEnsure the target server process is started and listening on the specified host and port before the proxy attempts to forward requests to it. Check firewall rules if the target is remote. -
Error: Must provide a target protocol and host (e.g., http://localhost:8000)
cause The `target` option required by `createProxyServer` or `proxy.web`/`proxy.ws` methods was either missing or malformed.fixProvide a complete target URL string including protocol, host, and port (e.g., `{ target: 'http://localhost:3000' }`) in the proxy options. -
Error: target server should listen on event 'upgrade' or be created by http.createServer.listen(port) with a 'ws' event.
cause Attempted to proxy a WebSocket connection (`proxy.ws`) to a target server that does not handle WebSocket `upgrade` requests, or the target server's WebSocket handling is improperly configured.fixEnsure the target server is capable of handling WebSocket connections and has an `upgrade` event listener or uses a library (like `ws` or `socket.io`) that manages WebSocket upgrades correctly.
Warnings
- breaking This package is a fork specifically designed for Node.js v16.x and later. While its `engines` field might indicate broader compatibility, using it on significantly older Node.js versions (e.g., < v14) may lead to unexpected behavior, compatibility issues, or even crashes due to underlying API changes or deprecations it aims to address for modern Node.js.
- gotcha Failing to handle `error` events emitted by the `ProxyServer` instance can cause your Node.js application to crash silently or with an unhandled promise rejection/exception. Errors can occur due to target server unavailability, network issues, or misconfigurations.
- gotcha As a fork, `http-proxy-node16` may not always be perfectly in sync with the latest features, bug fixes, or security patches released in the original `node-http-proxy` upstream project. Users should periodically check both projects for critical updates and potential divergences.
- gotcha The `proxy.listen(port)` method is a convenience wrapper for `http.createServer`. For more control over the underlying HTTP server's lifecycle, request handling, or to integrate with existing `http.Server` instances, it is generally recommended to create your own `http.createServer` and use `proxy.web(req, res, options)` or `proxy.ws(req, socket, head, options)` within its request listeners.
Install
-
npm install http-proxy-node16 -
yarn add http-proxy-node16 -
pnpm add http-proxy-node16
Imports
- createProxyServer
const createProxyServer = require('http-proxy-node16');import { createProxyServer } from 'http-proxy-node16'; - ProxyServer
import type { ProxyServer } from 'http-proxy-node16'; - ProxyServerOptions
import { ProxyServerOptions } from 'http-proxy-node16';import type { ProxyServerOptions } from 'http-proxy-node16'; - Full module object (CommonJS)
const httpProxy = require('http-proxy-node16');
Quickstart
const http = require('http');
const httpProxy = require('http-proxy');
// Create your proxy server and set the target in the options.
// It will listen on port 8000 and proxy requests to the target server on 9000.
const proxy = httpProxy.createProxyServer({ target: 'http://localhost:9000' });
proxy.listen(8000);
// Handle errors from the proxy server, essential for robust applications.
proxy.on('error', function (err, req, res) {
console.error('Proxy error:', err);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Something went wrong with the proxy.');
});
// Create your target server.
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
console.log('Proxy server listening on http://localhost:8000');
console.log('Target server listening on http://localhost:9000');
console.log('Try visiting http://localhost:8000 in your browser to see the proxy in action.');