HTTP and WebSocket Proxy for Node.js

0.5.1 · active · verified Wed Apr 22

httpxy is a performant and feature-rich HTTP and WebSocket proxy library for Node.js, currently at version `0.5.1`. It maintains an active release cadence with frequent minor and patch updates, as evidenced by recent versions like v0.5.1, v0.5.0, v0.4.0, and v0.3.0. The library offers three primary interfaces: `proxyFetch` for modern web-standard `Request`/`Response` based proxying, `proxyUpgrade` for standalone WebSocket upgrade handling without a full server instance, and `createProxyServer` which provides a more traditional, event-driven HTTP proxy server, originally forked from `node-http-proxy`. Key differentiators include its adoption of web-standard APIs, explicit WebSocket proxying capabilities, and performance optimizations inspired by libraries like `fast-proxy`. It ships with comprehensive TypeScript types, ensuring robust development in typed environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up a basic HTTP proxy server using `createProxyServer`. It listens on a specified port and forwards incoming requests to a configurable target URL, handling potential proxy errors gracefully.

import { createServer } from 'node:http';
import { createProxyServer } from 'httpxy';

const proxy = createProxyServer({});

const server = createServer(async (req, res) => {
  try {
    // Replace with your target server's address
    const targetAddress = process.env.PROXY_TARGET_URL ?? 'http://127.0.0.1:8080';
    await proxy.web(req, res, {
      target: targetAddress
    });
  } catch (error) {
    console.error('Proxy error:', error);
    res.statusCode = 500;
    res.end('Proxy error: ' + String(error));
  }
});

const port = process.env.PORT ?? 3000;
server.listen(port, () => {
  console.log(`Proxy is listening on http://localhost:${port}`);
  console.log('Ensure a target server is running at', process.env.PROXY_TARGET_URL || 'http://127.0.0.1:8080');
});

view raw JSON →