Skip to main content

Function Signature

Creates an HTTP proxy server that routes requests based on the Host header. Returns a ProxyServer (either http.Server or net.Server when TLS is enabled).

Parameters

ProxyServerOptions
required
Configuration object for the proxy server

Return Value

http.Server | net.Server
Returns a Node.js server instance that can be started with .listen(). When TLS is enabled, returns a net.Server that wraps both HTTP/2 and HTTP/1.1 servers.

Features

HTTP/2 Support

When TLS configuration is provided, the proxy server automatically:
  • Enables HTTP/2 with multiplexing for better performance
  • Falls back to HTTP/1.1 for WebSocket connections
  • Accepts both plain HTTP and HTTPS on the same port

WebSocket Support

The proxy transparently handles WebSocket upgrades:
  • Forwards the complete handshake including Sec-WebSocket-Accept headers
  • Preserves subprotocol negotiation
  • Maintains bidirectional streaming

Loop Detection

The proxy automatically detects and prevents forwarding loops:
  • Tracks request hops using the X-Portless-Hops header
  • Rejects requests after 5 hops with HTTP 508
  • Provides helpful error messages suggesting changeOrigin: true for dev server proxies

Automatic Header Forwarding

The proxy adds standard forwarding headers to all proxied requests:
  • X-Forwarded-For: Client IP address
  • X-Forwarded-Proto: Original protocol (http/https)
  • X-Forwarded-Host: Original Host header
  • X-Forwarded-Port: Original port number

Example: Basic HTTP Proxy

Example: HTTPS Proxy with TLS

Example: Dynamic Routing

Error Handling

The proxy server handles several error conditions automatically:

Type Definitions

RouteStore

Manage route mappings with persistence

Type Definitions

Complete type reference