Reconstruct the original URL used in an HTTP request based on the HTTP request headers.
The module takes into account potential URL rewrites made by proxies, load balancers, etc along the way (as long as these append special HTTP headers to the request).
Supported HTTP headers:
If the protocol (http vs https) cannot be determined based on the above
encrypted flag on the TLS connection is used.
npm install original-url --save
const http =const originalUrl =const server = httpserver
$ curl localhost:1337 Original URL: http://localhost:1337/ $ curl -H 'Host: example.com' localhost:1337 Original URL: http://example.com/ $ curl -H 'Host: example.com:1234' localhost:1337 Original URL: http://example.com:1234/ $ curl -H 'Forwarded: proto=https; host=example.com; for="10.0.0.1:1234"' localhost:1337/sub/path?key=value Original URL: https://example.com:1234/sub/path?key=value $ curl -H 'X-Forwarded-Host: example.com' -H 'X-Forwarded-Host: proxy.local' localhost:1337 Original URL: http://example.com/
result = originalUrl(req)
This module exposes a single function which takes an HTTP request object
in the form of
When called, the function returns a
result object with a
property containing the fully resolved URL. The
result object will
also contain any other property normally returned by the Node.js core
If the hostname for some reason cannot be determined,
not be present.