Node.js HTTP Proxy Server

4.0.0 · active · verified Wed Apr 22

The `proxy` package provides a programmatic HTTP(S) proxy server API for Node.js, allowing developers to script custom proxy logic. Its current stable version is `4.0.0`, which notably raised the minimum Node.js requirement to version 20. The project maintains an active release cadence, with recent updates across its associated `proxy-agent` ecosystem, ensuring compatibility and improvements. Key differentiators include its simple `createProxy` API for building custom proxy servers, similar to established solutions like Squid or Privoxy, but integrated directly within a Node.js environment. It supports both standard HTTP proxying and the HTTP `CONNECT` method for tunneling. The package also includes a companion CLI tool for quick server spawning.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up a basic HTTP(S) proxy server on port 3128 with a simple username/password authentication scheme using the `createProxy` API.

import * as http from 'http';
import { createProxy } from 'proxy';

const server = createProxy(http.createServer());

// Basic authorization example (replace with real auth for production)
server.authenticate = function (req, fn) {
  const auth = req.headers['proxy-authorization'];
  if (auth && auth.startsWith('Basic ')) {
    const credentials = Buffer.from(auth.slice(6), 'base64').toString().split(':');
    const [username, password] = credentials;
    if (username === 'user' && password === 'password') {
      return fn(null, true);
    }
  }
  fn(null, false);
};

server.listen(3128, () => {
  const address = server.address();
  const port = typeof address === 'string' ? address : address?.port;
  console.log('HTTP(s) proxy server listening on port %d', port);
});

view raw JSON →