Reconstruct Original HTTP Request URL

1.2.3 · maintenance · verified Tue Apr 21

The `original-url` package provides a utility function to reconstruct the full, original URL of an HTTP request by intelligently parsing various standard and de-facto standard HTTP headers. It accounts for potential modifications made by proxies, load balancers, and other intermediaries by examining headers like `Host`, `Forwarded`, `X-Forwarded-Proto`, `X-Forwarded-Host`, `X-Forwarded-Port`, and others. If the protocol cannot be determined from headers, it leverages the TLS connection's `encrypted` flag. The module returns a URL object compatible with Node.js's native `url.parse` output, including a `full` property for the complete URL string. The current stable version is 1.2.3, published over 6 years ago, indicating a very mature or slow-moving project that is likely in maintenance mode rather than active development. Its primary differentiator is its comprehensive handling of various proxy-related headers to accurately determine the client-facing URL.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up a simple Node.js HTTP server that uses `original-url` to determine and display the original URL of incoming requests, including detailed URL components parsed from various proxy headers.

import http from 'http';
import originalUrl from 'original-url';

const server = http.createServer(function (req, res) {
  const url = originalUrl(req);
  if (url.full) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end(`Original URL: ${url.full}\nProtocol: ${url.protocol}\nHost: ${url.host || 'N/A'}\nPath: ${url.pathname || 'N/A'}\nQuery: ${url.query || 'N/A'}\n`);
  } else {
    res.writeHead(400, { 'Content-Type': 'text/plain' });
    res.end('Original URL could not be determined. Check request headers.\n');
  }
});

server.listen(1337, () => {
  console.log('Server listening on http://localhost:1337');
  console.log('Try with: curl localhost:1337');
  console.log("Or with proxy headers: curl -H 'Forwarded: proto=https; host=example.com; for="10.0.0.1:1234"' localhost:1337/sub/path?key=value");
});

view raw JSON →