This utility function converts a URL object into an ordinary options object as expected by the http.request() and https.request() APIs.
http.request()
https.request()
import { urlToHttpOptions } from 'node:url';const myURL = new URL('https://a:b@測試?abc#foo');console.log(urlToHttpOptions(myURL));/*{ protocol: 'https:', hostname: 'xn--g6w251d', hash: '#foo', search: '?abc', pathname: '/', path: '/?abc', href: 'https://a:b@xn--g6w251d/?abc#foo', auth: 'a:b'} Copy
import { urlToHttpOptions } from 'node:url';const myURL = new URL('https://a:b@測試?abc#foo');console.log(urlToHttpOptions(myURL));/*{ protocol: 'https:', hostname: 'xn--g6w251d', hash: '#foo', search: '?abc', pathname: '/', path: '/?abc', href: 'https://a:b@xn--g6w251d/?abc#foo', auth: 'a:b'}
The WHATWG URL object to convert to an options object.
WHATWG URL
Options object
v15.7.0, v14.18.0
This utility function converts a URL object into an ordinary options object as expected by the
http.request()
andhttps.request()
APIs.