Returns a URL object from a Node.js IncomingMessage or Http2ServerRequest, accounting for proxy headers.
- Works with HTTP/1.1, HTTPS, and HTTP/2
- Supports
Forwarded(RFC 7239),X-Forwarded-Proto,X-Forwarded-Host,X-Forwarded-Port - Zero dependencies
import http from "node:http";
import http2 from "node:http2";
import {urlFromReq} from "url-from-req";
http.createServer((req, res) => {
res.end(urlFromReq(req).href); // "http://example.com/path?q=1"
}).listen(3000);
http2.createSecureServer({key, cert}, (req, res) => {
res.end(urlFromReq(req).href); // "https://example.com/path?q=1"
}).listen(3001);BSD-2-Clause