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.
npm install original-urlVerified import paths — ran on the pinned version, not inferred.
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.
Manually inspect the parsed URL properties and consider additional sanitization or validation logic if the application processes sensitive information or relies on strict URL format compliance. For critical applications, evaluate newer, actively maintained alternatives for URL parsing from proxy headers.
Ensure `original-url` is only used on the server-side within a Node.js HTTP server context where a standard `http.IncomingMessage` object is available. For other contexts, manually parse headers or use context-appropriate URL utilities.
Test thoroughly in your specific deployment environment. If issues arise, inspect the raw `req.headers` object within your serverless function or container to understand the format and availability of proxy headers. You may need to manually extract and reconstruct the URL based on your platform's specific header conventions (e.g., `x-forwarded-host`, `x-real-ip`).
Always use `original-url` behind a trusted proxy that correctly sets `Forwarded` headers or cleans potentially spoofed `X-` headers. Implement application-level validation to ensure the reconstructed URL is within expected domains or formats, especially when using it for redirects or security-sensitive operations.
Change the import statement to `import originalUrl from 'original-url';` for ES modules, or `const originalUrl = require('original-url');` for CommonJS.Ensure that the incoming HTTP request includes at least a `Host` header. If behind a proxy, confirm that the proxy correctly forwards or sets `Forwarded` or `X-Forwarded-*` headers. For HTTPS, verify that the TLS connection sets the `req.socket.encrypted` flag correctly or `X-Forwarded-Proto: https` is present.
Switch to ES module import syntax: `import originalUrl from 'original-url';`. If you need to use CommonJS modules within an ES module, consider dynamic `import()` or review Node.js's interoperability documentation.
No dependency data recorded yet.