Skip to content

silverwind/url-from-req

Repository files navigation

url-from-req

Returns a URL object from a Node.js IncomingMessage or Http2ServerRequest, accounting for proxy headers.

Features

  • Works with HTTP/1.1, HTTPS, and HTTP/2
  • Supports Forwarded (RFC 7239), X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port
  • Zero dependencies

Usage

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);

License

BSD-2-Clause

About

Reconstruct the original URL from a HTTP/1 or HTTP/2 request

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors