%PDF- %PDF-
Direktori : /usr/share/doc/nodejs/api/ |
Current File : //usr/share/doc/nodejs/api/url.html |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <meta name="nodejs.org:node-version" content="v18.19.1"> <title>URL | Node.js v18.19.1 Documentation</title> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/hljs.css"> <script async defer src="assets/api.js" type="text/javascript"></script> __JS_FLAVORED_DYNAMIC_CSS__ </head> <body class="alt apidoc" id="api-section-url"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="index.html" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> </ul> <hr class="line"/> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url active">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"/> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul> </div> <div id="column1" data-id="url" class="interior"> <header class="header"> <div class="header-container"> <h1>Node.js v18.19.1 documentation</h1> <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> <path fill="none" d="M0 0h24v24H0z" /> <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> </svg> <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> <path d="M0 0h24v24H0z" fill="none" /> <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> </svg> </button> </div> <div id="gtoc"> <ul> <li class="pinned-header">Node.js v18.19.1</li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Table of contents </a> <div class="picker"><div class="toc"><ul> <li><a href="#url">URL</a><ul> <li><a href="#url-strings-and-url-objects">URL strings and URL objects</a><ul> <li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li> </ul> </li> <li><a href="#the-whatwg-url-api">The WHATWG URL API</a><ul> <li><a href="#class-url">Class: <code>URL</code></a><ul> <li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li> <li><a href="#urlhash"><code>url.hash</code></a></li> <li><a href="#urlhost"><code>url.host</code></a></li> <li><a href="#urlhostname"><code>url.hostname</code></a></li> <li><a href="#urlhref"><code>url.href</code></a></li> <li><a href="#urlorigin"><code>url.origin</code></a></li> <li><a href="#urlpassword"><code>url.password</code></a></li> <li><a href="#urlpathname"><code>url.pathname</code></a></li> <li><a href="#urlport"><code>url.port</code></a></li> <li><a href="#urlprotocol"><code>url.protocol</code></a><ul> <li><a href="#special-schemes">Special schemes</a></li> </ul> </li> <li><a href="#urlsearch"><code>url.search</code></a></li> <li><a href="#urlsearchparams"><code>url.searchParams</code></a></li> <li><a href="#urlusername"><code>url.username</code></a></li> <li><a href="#urltostring"><code>url.toString()</code></a></li> <li><a href="#urltojson"><code>url.toJSON()</code></a></li> <li><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></li> <li><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></li> <li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li> </ul> </li> <li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a><ul> <li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li> <li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li> <li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li> <li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li> <li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li> <li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li> <li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li> <li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li> <li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li> <li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li> <li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li> <li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li> <li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li> <li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li> <li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li> <li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li> <li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li> <li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li> </ul> </li> <li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li> <li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li> <li><a href="#urlfileurltopathurl"><code>url.fileURLToPath(url)</code></a></li> <li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li> <li><a href="#urlpathtofileurlpath"><code>url.pathToFileURL(path)</code></a></li> <li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li> </ul> </li> <li><a href="#legacy-url-api">Legacy URL API</a><ul> <li><a href="#legacy-urlobject">Legacy <code>urlObject</code></a><ul> <li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li> <li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li> <li><a href="#urlobjecthost"><code>urlObject.host</code></a></li> <li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li> <li><a href="#urlobjecthref"><code>urlObject.href</code></a></li> <li><a href="#urlobjectpath"><code>urlObject.path</code></a></li> <li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li> <li><a href="#urlobjectport"><code>urlObject.port</code></a></li> <li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li> <li><a href="#urlobjectquery"><code>urlObject.query</code></a></li> <li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li> <li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li> </ul> </li> <li><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></li> <li><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></li> <li><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></li> </ul> </li> <li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a><ul> <li><a href="#legacy-api">Legacy API</a></li> <li><a href="#whatwg-api">WHATWG API</a></li> </ul> </li> </ul> </li> </ul> </div></div> </li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Index </a> <div class="picker"><ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> <li> <a href="index.html">Index</a> </li> </ul> <hr class="line"/> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url active">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"/> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul> </div> </li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Other versions </a> <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v18.x/api/url.html">18.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v17.x/api/url.html">17.x</a></li> <li><a href="https://nodejs.org/docs/latest-v16.x/api/url.html">16.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v15.x/api/url.html">15.x</a></li> <li><a href="https://nodejs.org/docs/latest-v14.x/api/url.html">14.x</a></li> <li><a href="https://nodejs.org/docs/latest-v13.x/api/url.html">13.x</a></li> <li><a href="https://nodejs.org/docs/latest-v12.x/api/url.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/url.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/url.html">10.x</a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/url.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/url.html">8.x</a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/url.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/url.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/url.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/url.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/url.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/url.html">0.10.x</a></li></ol></div> </li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Options </a> <div class="picker"> <ul> <li> <a href="all.html">View on single page</a> </li> <li> <a href="url.json">View as JSON</a> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/url.md">Edit on GitHub</a></li> </ul> </div> </li> </ul> </div> <hr> </header> <details id="toc" open><summary>Table of contents</summary><ul> <li><a href="#url">URL</a><ul> <li><a href="#url-strings-and-url-objects">URL strings and URL objects</a><ul> <li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li> </ul> </li> <li><a href="#the-whatwg-url-api">The WHATWG URL API</a><ul> <li><a href="#class-url">Class: <code>URL</code></a><ul> <li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li> <li><a href="#urlhash"><code>url.hash</code></a></li> <li><a href="#urlhost"><code>url.host</code></a></li> <li><a href="#urlhostname"><code>url.hostname</code></a></li> <li><a href="#urlhref"><code>url.href</code></a></li> <li><a href="#urlorigin"><code>url.origin</code></a></li> <li><a href="#urlpassword"><code>url.password</code></a></li> <li><a href="#urlpathname"><code>url.pathname</code></a></li> <li><a href="#urlport"><code>url.port</code></a></li> <li><a href="#urlprotocol"><code>url.protocol</code></a><ul> <li><a href="#special-schemes">Special schemes</a></li> </ul> </li> <li><a href="#urlsearch"><code>url.search</code></a></li> <li><a href="#urlsearchparams"><code>url.searchParams</code></a></li> <li><a href="#urlusername"><code>url.username</code></a></li> <li><a href="#urltostring"><code>url.toString()</code></a></li> <li><a href="#urltojson"><code>url.toJSON()</code></a></li> <li><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></li> <li><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></li> <li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li> </ul> </li> <li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a><ul> <li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li> <li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li> <li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li> <li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li> <li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li> <li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li> <li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li> <li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li> <li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li> <li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li> <li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li> <li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li> <li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li> <li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li> <li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li> <li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li> <li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li> <li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li> </ul> </li> <li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li> <li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li> <li><a href="#urlfileurltopathurl"><code>url.fileURLToPath(url)</code></a></li> <li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li> <li><a href="#urlpathtofileurlpath"><code>url.pathToFileURL(path)</code></a></li> <li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li> </ul> </li> <li><a href="#legacy-url-api">Legacy URL API</a><ul> <li><a href="#legacy-urlobject">Legacy <code>urlObject</code></a><ul> <li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li> <li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li> <li><a href="#urlobjecthost"><code>urlObject.host</code></a></li> <li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li> <li><a href="#urlobjecthref"><code>urlObject.href</code></a></li> <li><a href="#urlobjectpath"><code>urlObject.path</code></a></li> <li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li> <li><a href="#urlobjectport"><code>urlObject.port</code></a></li> <li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li> <li><a href="#urlobjectquery"><code>urlObject.query</code></a></li> <li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li> <li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li> </ul> </li> <li><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></li> <li><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></li> <li><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></li> </ul> </li> <li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a><ul> <li><a href="#legacy-api">Legacy API</a></li> <li><a href="#whatwg-api">WHATWG API</a></li> </ul> </li> </ul> </li> </ul> </details> <div id="apicontent"> <h2>URL<span><a class="mark" href="#url" id="url">#</a></span><a aria-hidden="true" class="legacy" id="url_url"></a></h2> <!--introduced_in=v0.10.0--> <blockquote> <p>Stability: 2 - Stable</p> </blockquote> <!-- source_link=lib/url.js --> <p>The <code>node:url</code> module provides utilities for URL resolution and parsing. It can be accessed using:</p> <pre><code class="language-mjs">import url from 'node:url'; </code></pre> <pre><code class="language-cjs">const url = require('node:url'); </code></pre> <section><h3>URL strings and URL objects<span><a class="mark" href="#url-strings-and-url-objects" id="url-strings-and-url-objects">#</a></span><a aria-hidden="true" class="legacy" id="url_url_strings_and_url_objects"></a></h3> <p>A URL string is a structured string containing multiple meaningful components. When parsed, a URL object is returned containing properties for each of these components.</p> <p>The <code>node:url</code> module provides two APIs for working with URLs: a legacy API that is Node.js specific, and a newer API that implements the same <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> used by web browsers.</p> <p>A comparison between the WHATWG and legacy APIs is provided below. Above the URL <code>'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>, properties of an object returned by the legacy <code>url.parse()</code> are shown. Below it are properties of a WHATWG <code>URL</code> object.</p> <p>WHATWG URL's <code>origin</code> property includes <code>protocol</code> and <code>host</code>, but not <code>username</code> or <code>password</code>.</p> <pre><code class="language-text">┌────────────────────────────────────────────────────────────────────────────────────────────────┐ │ href │ ├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤ │ protocol │ │ auth │ host │ path │ hash │ │ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │ │ │ │ │ hostname │ port │ pathname │ search │ │ │ │ │ │ │ │ ├─┬──────────────┤ │ │ │ │ │ │ │ │ │ query │ │ " https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash " │ │ │ │ │ hostname │ port │ │ │ │ │ │ │ │ ├─────────────────┴──────┤ │ │ │ │ protocol │ │ username │ password │ host │ │ │ │ ├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │ │ origin │ │ origin │ pathname │ search │ hash │ ├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤ │ href │ └────────────────────────────────────────────────────────────────────────────────────────────────┘ (All spaces in the "" line should be ignored. They are purely for formatting.) </code></pre> <p>Parsing the URL string using the WHATWG API:</p> <pre><code class="language-js">const myURL = new URL('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); </code></pre> <p>Parsing the URL string using the legacy API:</p> <pre><code class="language-mjs">import url from 'node:url'; const myURL = url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); </code></pre> <pre><code class="language-cjs">const url = require('node:url'); const myURL = url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); </code></pre> <h4>Constructing a URL from component parts and getting the constructed string<span><a class="mark" href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string" id="constructing-a-url-from-component-parts-and-getting-the-constructed-string">#</a></span><a aria-hidden="true" class="legacy" id="url_constructing_a_url_from_component_parts_and_getting_the_constructed_string"></a></h4> <p>It is possible to construct a WHATWG URL from component parts using either the property setters or a template literal string:</p> <pre><code class="language-js">const myURL = new URL('https://example.org'); myURL.pathname = '/a/b/c'; myURL.search = '?d=e'; myURL.hash = '#fgh'; </code></pre> <pre><code class="language-js">const pathname = '/a/b/c'; const search = '?d=e'; const hash = '#fgh'; const myURL = new URL(`https://example.org${pathname}${search}${hash}`); </code></pre> <p>To get the constructed URL string, use the <code>href</code> property accessor:</p> <pre><code class="language-js">console.log(myURL.href); </code></pre> </section><section><h3>The WHATWG URL API<span><a class="mark" href="#the-whatwg-url-api" id="the-whatwg-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_the_whatwg_url_api"></a></h3> <h4>Class: <code>URL</code><span><a class="mark" href="#class-url" id="class-url">#</a></span><a aria-hidden="true" class="legacy" id="url_class_url"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The class is now available on the global object.</p> </td></tr> <tr><td>v7.0.0, v6.13.0</td> <td><p><span>Added in: v7.0.0, v6.13.0</span></p> </td></tr> </table> </details> </div><p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> may be found in the Standard itself. The <code>URL</code> class is also available on the global object.</p> <p>In accordance with browser conventions, all properties of <code>URL</code> objects are implemented as getters and setters on the class prototype, rather than as data properties on the object itself. Thus, unlike <a href="#legacy-urlobject">legacy <code>urlObject</code></a>s, using the <code>delete</code> keyword on any properties of <code>URL</code> objects (e.g. <code>delete myURL.protocol</code>, <code>delete myURL.pathname</code>, etc) has no effect but will still return <code>true</code>.</p> <h5><code>new URL(input[, base])</code><span><a class="mark" href="#new-urlinput-base" id="new-urlinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_new_url_input_base"></a></h5> <!-- changes: - version: v18.17.0 pr-url: https://github.com/nodejs/node/pull/47339 description: ICU requirement is removed. --> <ul> <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute or relative input URL to parse. If <code>input</code> is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code> is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> </ul> <p>Creates a new <code>URL</code> object by parsing the <code>input</code> relative to the <code>base</code>. If <code>base</code> is passed as a string, it will be parsed equivalent to <code>new URL(base)</code>.</p> <pre><code class="language-js">const myURL = new URL('/foo', 'https://example.org/'); // https://example.org/foo </code></pre> <p>The URL constructor is accessible as a property on the global object. It can also be imported from the built-in url module:</p> <pre><code class="language-mjs">import { URL } from 'node:url'; console.log(URL === globalThis.URL); // Prints 'true'. </code></pre> <pre><code class="language-cjs">console.log(URL === require('node:url').URL); // Prints 'true'. </code></pre> <p>A <code>TypeError</code> will be thrown if the <code>input</code> or <code>base</code> are not valid URLs. Note that an effort will be made to coerce the given values into strings. For instance:</p> <pre><code class="language-js">const myURL = new URL({ toString: () => 'https://example.org/' }); // https://example.org/ </code></pre> <p>Unicode characters appearing within the host name of <code>input</code> will be automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm.</p> <pre><code class="language-js">const myURL = new URL('https://測試'); // https://xn--g6w251d/ </code></pre> <p>In cases where it is not known in advance if <code>input</code> is an absolute URL and a <code>base</code> is provided, it is advised to validate that the <code>origin</code> of the <code>URL</code> object is what is expected.</p> <pre><code class="language-js">let myURL = new URL('http://Example.com/', 'https://example.org/'); // http://example.com/ myURL = new URL('https://Example.com/', 'https://example.org/'); // https://example.com/ myURL = new URL('foo://Example.com/', 'https://example.org/'); // foo://Example.com/ myURL = new URL('http:Example.com/', 'https://example.org/'); // http://example.com/ myURL = new URL('https:Example.com/', 'https://example.org/'); // https://example.org/Example.com/ myURL = new URL('foo:Example.com/', 'https://example.org/'); // foo:Example.com/ </code></pre> <h5><code>url.hash</code><span><a class="mark" href="#urlhash" id="urlhash">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hash"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the fragment portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/foo#bar'); console.log(myURL.hash); // Prints #bar myURL.hash = 'baz'; console.log(myURL.href); // Prints https://example.org/foo#baz </code></pre> <p>Invalid URL characters included in the value assigned to the <code>hash</code> property are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> <h5><code>url.host</code><span><a class="mark" href="#urlhost" id="urlhost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_host"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the host portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org:81/foo'); console.log(myURL.host); // Prints example.org:81 myURL.host = 'example.com:82'; console.log(myURL.href); // Prints https://example.com:82/foo </code></pre> <p>Invalid host values assigned to the <code>host</code> property are ignored.</p> <h5><code>url.hostname</code><span><a class="mark" href="#urlhostname" id="urlhostname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hostname"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the host name portion of the URL. The key difference between <code>url.host</code> and <code>url.hostname</code> is that <code>url.hostname</code> does <em>not</em> include the port.</p> <pre><code class="language-js">const myURL = new URL('https://example.org:81/foo'); console.log(myURL.hostname); // Prints example.org // Setting the hostname does not change the port myURL.hostname = 'example.com'; console.log(myURL.href); // Prints https://example.com:81/foo // Use myURL.host to change the hostname and port myURL.host = 'example.org:82'; console.log(myURL.href); // Prints https://example.org:82/foo </code></pre> <p>Invalid host name values assigned to the <code>hostname</code> property are ignored.</p> <h5><code>url.href</code><span><a class="mark" href="#urlhref" id="urlhref">#</a></span><a aria-hidden="true" class="legacy" id="url_url_href"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the serialized URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/foo'); console.log(myURL.href); // Prints https://example.org/foo myURL.href = 'https://example.com/bar'; console.log(myURL.href); // Prints https://example.com/bar </code></pre> <p>Getting the value of the <code>href</code> property is equivalent to calling <a href="#urltostring"><code>url.toString()</code></a>.</p> <p>Setting the value of this property to a new value is equivalent to creating a new <code>URL</code> object using <a href="#new-urlinput-base"><code>new URL(value)</code></a>. Each of the <code>URL</code> object's properties will be modified.</p> <p>If the value assigned to the <code>href</code> property is not a valid URL, a <code>TypeError</code> will be thrown.</p> <h5><code>url.origin</code><span><a class="mark" href="#urlorigin" id="urlorigin">#</a></span><a aria-hidden="true" class="legacy" id="url_url_origin"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.0.0</td> <td><p>The scheme "gopher" is no longer special and <code>url.origin</code> now returns <code>'null'</code> for it.</p> </td></tr> </table> </details> </div><ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets the read-only serialization of the URL's origin.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/foo/bar?baz'); console.log(myURL.origin); // Prints https://example.org </code></pre> <pre><code class="language-js">const idnURL = new URL('https://測試'); console.log(idnURL.origin); // Prints https://xn--g6w251d console.log(idnURL.hostname); // Prints xn--g6w251d </code></pre> <h5><code>url.password</code><span><a class="mark" href="#urlpassword" id="urlpassword">#</a></span><a aria-hidden="true" class="legacy" id="url_url_password"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the password portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://abc:xyz@example.com'); console.log(myURL.password); // Prints xyz myURL.password = '123'; console.log(myURL.href); // Prints https://abc:123@example.com/ </code></pre> <p>Invalid URL characters included in the value assigned to the <code>password</code> property are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> <h5><code>url.pathname</code><span><a class="mark" href="#urlpathname" id="urlpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathname"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the path portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/abc/xyz?123'); console.log(myURL.pathname); // Prints /abc/xyz myURL.pathname = '/abcdef'; console.log(myURL.href); // Prints https://example.org/abcdef?123 </code></pre> <p>Invalid URL characters included in the value assigned to the <code>pathname</code> property are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> <h5><code>url.port</code><span><a class="mark" href="#urlport" id="urlport">#</a></span><a aria-hidden="true" class="legacy" id="url_url_port"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.0.0</td> <td><p>The scheme "gopher" is no longer special.</p> </td></tr> </table> </details> </div><ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the port portion of the URL.</p> <p>The port value may be a number or a string containing a number in the range <code>0</code> to <code>65535</code> (inclusive). Setting the value to the default port of the <code>URL</code> objects given <code>protocol</code> will result in the <code>port</code> value becoming the empty string (<code>''</code>).</p> <p>The port value can be an empty string in which case the port depends on the protocol/scheme:</p> <table> <thead> <tr> <th>protocol</th> <th>port</th> </tr> </thead> <tbody><tr> <td>"ftp"</td> <td>21</td> </tr> <tr> <td>"file"</td> <td></td> </tr> <tr> <td>"http"</td> <td>80</td> </tr> <tr> <td>"https"</td> <td>443</td> </tr> <tr> <td>"ws"</td> <td>80</td> </tr> <tr> <td>"wss"</td> <td>443</td> </tr> </tbody></table> <p>Upon assigning a value to the port, the value will first be converted to a string using <code>.toString()</code>.</p> <p>If that string is invalid but it begins with a number, the leading number is assigned to <code>port</code>. If the number lies outside the range denoted above, it is ignored.</p> <pre><code class="language-js">const myURL = new URL('https://example.org:8888'); console.log(myURL.port); // Prints 8888 // Default ports are automatically transformed to the empty string // (HTTPS protocol's default port is 443) myURL.port = '443'; console.log(myURL.port); // Prints the empty string console.log(myURL.href); // Prints https://example.org/ myURL.port = 1234; console.log(myURL.port); // Prints 1234 console.log(myURL.href); // Prints https://example.org:1234/ // Completely invalid port strings are ignored myURL.port = 'abcd'; console.log(myURL.port); // Prints 1234 // Leading numbers are treated as a port number myURL.port = '5678abcd'; console.log(myURL.port); // Prints 5678 // Non-integers are truncated myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234 // Out-of-range numbers which are not represented in scientific notation // will be ignored. myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); // Prints 1234 </code></pre> <p>Numbers which contain a decimal point, such as floating-point numbers or numbers in scientific notation, are not an exception to this rule. Leading numbers up to the decimal point will be set as the URL's port, assuming they are valid:</p> <pre><code class="language-js">myURL.port = 4.567e21; console.log(myURL.port); // Prints 4 (because it is the leading number in the string '4.567e21') </code></pre> <h5><code>url.protocol</code><span><a class="mark" href="#urlprotocol" id="urlprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_url_protocol"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the protocol portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org'); console.log(myURL.protocol); // Prints https: myURL.protocol = 'ftp'; console.log(myURL.href); // Prints ftp://example.org/ </code></pre> <p>Invalid URL protocol values assigned to the <code>protocol</code> property are ignored.</p> <h6>Special schemes<span><a class="mark" href="#special-schemes" id="special-schemes">#</a></span><a aria-hidden="true" class="legacy" id="url_special_schemes"></a></h6> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.0.0</td> <td><p>The scheme "gopher" is no longer special.</p> </td></tr> </table> </details> </div><p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> considers a handful of URL protocol schemes to be <em>special</em> in terms of how they are parsed and serialized. When a URL is parsed using one of these special protocols, the <code>url.protocol</code> property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa.</p> <p>For instance, changing from <code>http</code> to <code>https</code> works:</p> <pre><code class="language-js">const u = new URL('http://example.org'); u.protocol = 'https'; console.log(u.href); // https://example.org/ </code></pre> <p>However, changing from <code>http</code> to a hypothetical <code>fish</code> protocol does not because the new protocol is not special.</p> <pre><code class="language-js">const u = new URL('http://example.org'); u.protocol = 'fish'; console.log(u.href); // http://example.org/ </code></pre> <p>Likewise, changing from a non-special protocol to a special protocol is also not permitted:</p> <pre><code class="language-js">const u = new URL('fish://example.org'); u.protocol = 'http'; console.log(u.href); // fish://example.org </code></pre> <p>According to the WHATWG URL Standard, special protocol schemes are <code>ftp</code>, <code>file</code>, <code>http</code>, <code>https</code>, <code>ws</code>, and <code>wss</code>.</p> <h5><code>url.search</code><span><a class="mark" href="#urlsearch" id="urlsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_url_search"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the serialized query portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/abc?123'); console.log(myURL.search); // Prints ?123 myURL.search = 'abc=xyz'; console.log(myURL.href); // Prints https://example.org/abc?abc=xyz </code></pre> <p>Any invalid URL characters appearing in the value assigned the <code>search</code> property will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> <h5><code>url.searchParams</code><span><a class="mark" href="#urlsearchparams" id="urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_url_searchparams"></a></h5> <ul> <li><a href="url.html#class-urlsearchparams" class="type"><URLSearchParams></a></li> </ul> <p>Gets the <a href="#class-urlsearchparams"><code>URLSearchParams</code></a> object representing the query parameters of the URL. This property is read-only but the <code>URLSearchParams</code> object it provides can be used to mutate the URL instance; to replace the entirety of query parameters of the URL, use the <a href="#urlsearch"><code>url.search</code></a> setter. See <a href="#class-urlsearchparams"><code>URLSearchParams</code></a> documentation for details.</p> <p>Use care when using <code>.searchParams</code> to modify the <code>URL</code> because, per the WHATWG specification, the <code>URLSearchParams</code> object uses different rules to determine which characters to percent-encode. For instance, the <code>URL</code> object will not percent encode the ASCII tilde (<code>~</code>) character, while <code>URLSearchParams</code> will always encode it:</p> <pre><code class="language-js">const myURL = new URL('https://example.org/abc?foo=~bar'); console.log(myURL.search); // prints ?foo=~bar // Modify the URL via searchParams... myURL.searchParams.sort(); console.log(myURL.search); // prints ?foo=%7Ebar </code></pre> <h5><code>url.username</code><span><a class="mark" href="#urlusername" id="urlusername">#</a></span><a aria-hidden="true" class="legacy" id="url_url_username"></a></h5> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Gets and sets the username portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://abc:xyz@example.com'); console.log(myURL.username); // Prints abc myURL.username = '123'; console.log(myURL.href); // Prints https://123:xyz@example.com/ </code></pre> <p>Any invalid URL characters appearing in the value assigned the <code>username</code> property will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> <h5><code>url.toString()</code><span><a class="mark" href="#urltostring" id="urltostring">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tostring"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The <code>toString()</code> method on the <code>URL</code> object returns the serialized URL. The value returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and <a href="#urltojson"><code>url.toJSON()</code></a>.</p> <h5><code>url.toJSON()</code><span><a class="mark" href="#urltojson" id="urltojson">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tojson"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The <code>toJSON()</code> method on the <code>URL</code> object returns the serialized URL. The value returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and <a href="#urltostring"><code>url.toString()</code></a>.</p> <p>This method is automatically called when an <code>URL</code> object is serialized with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p> <pre><code class="language-js">const myURLs = [ new URL('https://www.example.com'), new URL('https://test.example.org'), ]; console.log(JSON.stringify(myURLs)); // Prints ["https://www.example.com/","https://test.example.org/"] </code></pre> <h5><code>URL.createObjectURL(blob)</code><span><a class="mark" href="#urlcreateobjecturlblob" id="urlcreateobjecturlblob">#</a></span><a aria-hidden="true" class="legacy" id="url_url_createobjecturl_blob"></a></h5> <div class="api_metadata"> <span>Added in: v16.7.0</span> </div><blockquote> <p>Stability: 1 - Experimental</p> </blockquote> <ul> <li><code>blob</code> <a href="buffer.html#class-blob" class="type"><Blob></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Creates a <code>'blob:nodedata:...'</code> URL string that represents the given <a href="buffer.html#class-blob" class="type"><Blob></a> object and can be used to retrieve the <code>Blob</code> later.</p> <pre><code class="language-js">const { Blob, resolveObjectURL, } = require('node:buffer'); const blob = new Blob(['hello']); const id = URL.createObjectURL(blob); // later... const otherBlob = resolveObjectURL(id); console.log(otherBlob.size); </code></pre> <p>The data stored by the registered <a href="buffer.html#class-blob" class="type"><Blob></a> will be retained in memory until <code>URL.revokeObjectURL()</code> is called to remove it.</p> <p><code>Blob</code> objects are registered within the current thread. If using Worker Threads, <code>Blob</code> objects registered within one Worker will not be available to other workers or the main thread.</p> <h5><code>URL.revokeObjectURL(id)</code><span><a class="mark" href="#urlrevokeobjecturlid" id="urlrevokeobjecturlid">#</a></span><a aria-hidden="true" class="legacy" id="url_url_revokeobjecturl_id"></a></h5> <div class="api_metadata"> <span>Added in: v16.7.0</span> </div><blockquote> <p>Stability: 1 - Experimental</p> </blockquote> <ul> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>'blob:nodedata:...</code> URL string returned by a prior call to <code>URL.createObjectURL()</code>.</li> </ul> <p>Removes the stored <a href="buffer.html#class-blob" class="type"><Blob></a> identified by the given ID. Attempting to revoke a ID that isn't registered will silently fail.</p> <h5><code>URL.canParse(input[, base])</code><span><a class="mark" href="#urlcanparseinput-base" id="urlcanparseinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_url_canparse_input_base"></a></h5> <div class="api_metadata"> <span>Added in: v18.17.0</span> </div><ul> <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute or relative input URL to parse. If <code>input</code> is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code> is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Checks if an <code>input</code> relative to the <code>base</code> can be parsed to a <code>URL</code>.</p> <pre><code class="language-js">const isValid = URL.canParse('/foo', 'https://example.org/'); // true const isNotValid = URL.canParse('/foo'); // false </code></pre> <h4>Class: <code>URLSearchParams</code><span><a class="mark" href="#class-urlsearchparams" id="class-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_class_urlsearchparams"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The class is now available on the global object.</p> </td></tr> <tr><td>v7.5.0, v6.13.0</td> <td><p><span>Added in: v7.5.0, v6.13.0</span></p> </td></tr> </table> </details> </div><p>The <code>URLSearchParams</code> API provides read and write access to the query of a <code>URL</code>. The <code>URLSearchParams</code> class can also be used standalone with one of the four following constructors. The <code>URLSearchParams</code> class is also available on the global object.</p> <p>The WHATWG <code>URLSearchParams</code> interface and the <a href="querystring.html"><code>querystring</code></a> module have similar purpose, but the purpose of the <a href="querystring.html"><code>querystring</code></a> module is more general, as it allows the customization of delimiter characters (<code>&</code> and <code>=</code>). On the other hand, this API is designed purely for URL query strings.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/?abc=123'); console.log(myURL.searchParams.get('abc')); // Prints 123 myURL.searchParams.append('abc', 'xyz'); console.log(myURL.href); // Prints https://example.org/?abc=123&abc=xyz myURL.searchParams.delete('abc'); myURL.searchParams.set('a', 'b'); console.log(myURL.href); // Prints https://example.org/?a=b const newSearchParams = new URLSearchParams(myURL.searchParams); // The above is equivalent to // const newSearchParams = new URLSearchParams(myURL.search); newSearchParams.append('a', 'c'); console.log(myURL.href); // Prints https://example.org/?a=b console.log(newSearchParams.toString()); // Prints a=b&a=c // newSearchParams.toString() is implicitly called myURL.search = newSearchParams; console.log(myURL.href); // Prints https://example.org/?a=b&a=c newSearchParams.delete('a'); console.log(myURL.href); // Prints https://example.org/?a=b&a=c </code></pre> <h5><code>new URLSearchParams()</code><span><a class="mark" href="#new-urlsearchparams" id="new-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams"></a></h5> <p>Instantiate a new empty <code>URLSearchParams</code> object.</p> <h5><code>new URLSearchParams(string)</code><span><a class="mark" href="#new-urlsearchparamsstring" id="new-urlsearchparamsstring">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_string"></a></h5> <ul> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A query string</li> </ul> <p>Parse the <code>string</code> as a query string, and use it to instantiate a new <code>URLSearchParams</code> object. A leading <code>'?'</code>, if present, is ignored.</p> <pre><code class="language-js">let params; params = new URLSearchParams('user=abc&query=xyz'); console.log(params.get('user')); // Prints 'abc' console.log(params.toString()); // Prints 'user=abc&query=xyz' params = new URLSearchParams('?user=abc&query=xyz'); console.log(params.toString()); // Prints 'user=abc&query=xyz' </code></pre> <h5><code>new URLSearchParams(obj)</code><span><a class="mark" href="#new-urlsearchparamsobj" id="new-urlsearchparamsobj">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_obj"></a></h5> <div class="api_metadata"> <span>Added in: v7.10.0, v6.13.0</span> </div><ul> <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object representing a collection of key-value pairs</li> </ul> <p>Instantiate a new <code>URLSearchParams</code> object with a query hash map. The key and value of each property of <code>obj</code> are always coerced to strings.</p> <p>Unlike <a href="querystring.html"><code>querystring</code></a> module, duplicate keys in the form of array values are not allowed. Arrays are stringified using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString"><code>array.toString()</code></a>, which simply joins all array elements with commas.</p> <pre><code class="language-js">const params = new URLSearchParams({ user: 'abc', query: ['first', 'second'], }); console.log(params.getAll('query')); // Prints [ 'first,second' ] console.log(params.toString()); // Prints 'user=abc&query=first%2Csecond' </code></pre> <h5><code>new URLSearchParams(iterable)</code><span><a class="mark" href="#new-urlsearchparamsiterable" id="new-urlsearchparamsiterable">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_iterable"></a></h5> <div class="api_metadata"> <span>Added in: v7.10.0, v6.13.0</span> </div><ul> <li><code>iterable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> An iterable object whose elements are key-value pairs</li> </ul> <p>Instantiate a new <code>URLSearchParams</code> object with an iterable map in a way that is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a>'s constructor. <code>iterable</code> can be an <code>Array</code> or any iterable object. That means <code>iterable</code> can be another <code>URLSearchParams</code>, in which case the constructor will simply create a clone of the provided <code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can themselves be any iterable object.</p> <p>Duplicate keys are allowed.</p> <pre><code class="language-js">let params; // Using an array params = new URLSearchParams([ ['user', 'abc'], ['query', 'first'], ['query', 'second'], ]); console.log(params.toString()); // Prints 'user=abc&query=first&query=second' // Using a Map object const map = new Map(); map.set('user', 'abc'); map.set('query', 'xyz'); params = new URLSearchParams(map); console.log(params.toString()); // Prints 'user=abc&query=xyz' // Using a generator function function* getQueryPairs() { yield ['user', 'abc']; yield ['query', 'first']; yield ['query', 'second']; } params = new URLSearchParams(getQueryPairs()); console.log(params.toString()); // Prints 'user=abc&query=first&query=second' // Each key-value pair must have exactly two elements new URLSearchParams([ ['user', 'abc', 'error'], ]); // Throws TypeError [ERR_INVALID_TUPLE]: // Each query pair must be an iterable [name, value] tuple </code></pre> <h5><code>urlSearchParams.append(name, value)</code><span><a class="mark" href="#urlsearchparamsappendname-value" id="urlsearchparamsappendname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_append_name_value"></a></h5> <ul> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Append a new name-value pair to the query string.</p> <h5><code>urlSearchParams.delete(name[, value])</code><span><a class="mark" href="#urlsearchparamsdeletename-value" id="urlsearchparamsdeletename-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_delete_name_value"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.18.0</td> <td><p>Add support for optional <code>value</code> argument.</p> </td></tr> </table> </details> </div><ul> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>If <code>value</code> is provided, removes all name-value pairs where name is <code>name</code> and value is <code>value</code>..</p> <p>If <code>value</code> is not provided, removes all name-value pairs whose name is <code>name</code>.</p> <h5><code>urlSearchParams.entries()</code><span><a class="mark" href="#urlsearchparamsentries" id="urlsearchparamsentries">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_entries"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query. Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code> is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p> <p>Alias for <a href="#urlsearchparamssymboliterator"><code>urlSearchParams[@@iterator]()</code></a>.</p> <h5><code>urlSearchParams.forEach(fn[, thisArg])</code><span><a class="mark" href="#urlsearchparamsforeachfn-thisarg" id="urlsearchparamsforeachfn-thisarg">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_foreach_fn_thisarg"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <td><p>Passing an invalid callback to the <code>fn</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p> </td></tr> </table> </details> </div><ul> <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked for each name-value pair in the query</li> <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> To be used as <code>this</code> value for when <code>fn</code> is called</li> </ul> <p>Iterates over each name-value pair in the query and invokes the given function.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/?a=b&c=d'); myURL.searchParams.forEach((value, name, searchParams) => { console.log(name, value, myURL.searchParams === searchParams); }); // Prints: // a b true // c d true </code></pre> <h5><code>urlSearchParams.get(name)</code><span><a class="mark" href="#urlsearchparamsgetname" id="urlsearchparamsgetname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_get_name"></a></h5> <ul> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> or <code>null</code> if there is no name-value pair with the given <code>name</code>.</li> </ul> <p>Returns the value of the first name-value pair whose name is <code>name</code>. If there are no such pairs, <code>null</code> is returned.</p> <h5><code>urlSearchParams.getAll(name)</code><span><a class="mark" href="#urlsearchparamsgetallname" id="urlsearchparamsgetallname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_getall_name"></a></h5> <ul> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> </ul> <p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are no such pairs, an empty array is returned.</p> <h5><code>urlSearchParams.has(name[, value])</code><span><a class="mark" href="#urlsearchparamshasname-value" id="urlsearchparamshasname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_has_name_value"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.18.0</td> <td><p>Add support for optional <code>value</code> argument.</p> </td></tr> </table> </details> </div><ul> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Checks if the <code>URLSearchParams</code> object contains key-value pair(s) based on <code>name</code> and an optional <code>value</code> argument.</p> <p>If <code>value</code> is provided, returns <code>true</code> when name-value pair with same <code>name</code> and <code>value</code> exists.</p> <p>If <code>value</code> is not provided, returns <code>true</code> if there is at least one name-value pair whose name is <code>name</code>.</p> <h5><code>urlSearchParams.keys()</code><span><a class="mark" href="#urlsearchparamskeys" id="urlsearchparamskeys">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_keys"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p> <pre><code class="language-js">const params = new URLSearchParams('foo=bar&foo=baz'); for (const name of params.keys()) { console.log(name); } // Prints: // foo // foo </code></pre> <h5><code>urlSearchParams.set(name, value)</code><span><a class="mark" href="#urlsearchparamssetname-value" id="urlsearchparamssetname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_set_name_value"></a></h5> <ul> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Sets the value in the <code>URLSearchParams</code> object associated with <code>name</code> to <code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>, set the first such pair's value to <code>value</code> and remove all others. If not, append the name-value pair to the query string.</p> <pre><code class="language-js">const params = new URLSearchParams(); params.append('foo', 'bar'); params.append('foo', 'baz'); params.append('abc', 'def'); console.log(params.toString()); // Prints foo=bar&foo=baz&abc=def params.set('foo', 'def'); params.set('xyz', 'opq'); console.log(params.toString()); // Prints foo=def&abc=def&xyz=opq </code></pre> <h5><code>urlSearchParams.size</code><span><a class="mark" href="#urlsearchparamssize" id="urlsearchparamssize">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_size"></a></h5> <div class="api_metadata"> <span>Added in: v18.16.0</span> </div><p>The total number of parameter entries.</p> <h5><code>urlSearchParams.sort()</code><span><a class="mark" href="#urlsearchparamssort" id="urlsearchparamssort">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_sort"></a></h5> <div class="api_metadata"> <span>Added in: v7.7.0, v6.13.0</span> </div><p>Sort all existing name-value pairs in-place by their names. Sorting is done with a <a href="https://en.wikipedia.org/wiki/Sorting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs with the same name is preserved.</p> <p>This method can be used, in particular, to increase cache hits.</p> <pre><code class="language-js">const params = new URLSearchParams('query[]=abc&type=search&query[]=123'); params.sort(); console.log(params.toString()); // Prints query%5B%5D=abc&query%5B%5D=123&type=search </code></pre> <h5><code>urlSearchParams.toString()</code><span><a class="mark" href="#urlsearchparamstostring" id="urlsearchparamstostring">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_tostring"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Returns the search parameters serialized as a string, with characters percent-encoded where necessary.</p> <h5><code>urlSearchParams.values()</code><span><a class="mark" href="#urlsearchparamsvalues" id="urlsearchparamsvalues">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_values"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p> <h5><code>urlSearchParams[Symbol.iterator]()</code><span><a class="mark" href="#urlsearchparamssymboliterator" id="urlsearchparamssymboliterator">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_symbol_iterator"></a></h5> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string. Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code> is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p> <p>Alias for <a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a>.</p> <pre><code class="language-js">const params = new URLSearchParams('foo=bar&xyz=baz'); for (const [name, value] of params) { console.log(name, value); } // Prints: // foo bar // xyz baz </code></pre> <h4><code>url.domainToASCII(domain)</code><span><a class="mark" href="#urldomaintoasciidomain" id="urldomaintoasciidomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintoascii_domain"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.17.0</td> <td><p>ICU requirement is removed.</p> </td></tr> <tr><td>v7.4.0, v6.13.0</td> <td><p><span>Added in: v7.4.0, v6.13.0</span></p> </td></tr> </table> </details> </div><ul> <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Returns the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> ASCII serialization of the <code>domain</code>. If <code>domain</code> is an invalid domain, the empty string is returned.</p> <p>It performs the inverse operation to <a href="#urldomaintounicodedomain"><code>url.domainToUnicode()</code></a>.</p> <pre><code class="language-mjs">import url from 'node:url'; console.log(url.domainToASCII('español.com')); // Prints xn--espaol-zwa.com console.log(url.domainToASCII('中文.com')); // Prints xn--fiq228c.com console.log(url.domainToASCII('xn--iñvalid.com')); // Prints an empty string </code></pre> <pre><code class="language-cjs">const url = require('node:url'); console.log(url.domainToASCII('español.com')); // Prints xn--espaol-zwa.com console.log(url.domainToASCII('中文.com')); // Prints xn--fiq228c.com console.log(url.domainToASCII('xn--iñvalid.com')); // Prints an empty string </code></pre> <h4><code>url.domainToUnicode(domain)</code><span><a class="mark" href="#urldomaintounicodedomain" id="urldomaintounicodedomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintounicode_domain"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.17.0</td> <td><p>ICU requirement is removed.</p> </td></tr> <tr><td>v7.4.0, v6.13.0</td> <td><p><span>Added in: v7.4.0, v6.13.0</span></p> </td></tr> </table> </details> </div><ul> <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Returns the Unicode serialization of the <code>domain</code>. If <code>domain</code> is an invalid domain, the empty string is returned.</p> <p>It performs the inverse operation to <a href="#urldomaintoasciidomain"><code>url.domainToASCII()</code></a>.</p> <pre><code class="language-mjs">import url from 'node:url'; console.log(url.domainToUnicode('xn--espaol-zwa.com')); // Prints español.com console.log(url.domainToUnicode('xn--fiq228c.com')); // Prints 中文.com console.log(url.domainToUnicode('xn--iñvalid.com')); // Prints an empty string </code></pre> <pre><code class="language-cjs">const url = require('node:url'); console.log(url.domainToUnicode('xn--espaol-zwa.com')); // Prints español.com console.log(url.domainToUnicode('xn--fiq228c.com')); // Prints 中文.com console.log(url.domainToUnicode('xn--iñvalid.com')); // Prints an empty string </code></pre> <h4><code>url.fileURLToPath(url)</code><span><a class="mark" href="#urlfileurltopathurl" id="urlfileurltopathurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_fileurltopath_url"></a></h4> <div class="api_metadata"> <span>Added in: v10.12.0</span> </div><ul> <li><code>url</code> {URL | string} The file URL string or URL object to convert to a path.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The fully-resolved platform-specific Node.js file path.</li> </ul> <p>This function ensures the correct decodings of percent-encoded characters as well as ensuring a cross-platform valid absolute path string.</p> <pre><code class="language-mjs">import { fileURLToPath } from 'node:url'; const __filename = fileURLToPath(import.meta.url); new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows) new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) new URL('file:///hello world').pathname; // Incorrect: /hello%20world fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) </code></pre> <pre><code class="language-cjs">const { fileURLToPath } = require('node:url'); new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows) new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) new URL('file:///hello world').pathname; // Incorrect: /hello%20world fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) </code></pre> <h4><code>url.format(URL[, options])</code><span><a class="mark" href="#urlformaturl-options" id="urlformaturl-options">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_url_options"></a></h4> <div class="api_metadata"> <span>Added in: v7.6.0</span> </div><ul> <li><code>URL</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> A <a href="#the-whatwg-url-api">WHATWG URL</a> object</li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the serialized URL string should include the username and password, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li> <li><code>fragment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the serialized URL string should include the fragment, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li> <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the serialized URL string should include the search query, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li> <li><code>unicode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if Unicode characters appearing in the host component of the URL string should be encoded directly as opposed to being Punycode encoded. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Returns a customizable serialization of a URL <code>String</code> representation of a <a href="#the-whatwg-url-api">WHATWG URL</a> object.</p> <p>The URL object has both a <code>toString()</code> method and <code>href</code> property that return string serializations of the URL. These are not, however, customizable in any way. The <code>url.format(URL[, options])</code> method allows for basic customization of the output.</p> <pre><code class="language-mjs">import url from 'node:url'; const myURL = new URL('https://a:b@測試?abc#foo'); console.log(myURL.href); // Prints https://a:b@xn--g6w251d/?abc#foo console.log(myURL.toString()); // Prints https://a:b@xn--g6w251d/?abc#foo console.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); // Prints 'https://測試/?abc' </code></pre> <pre><code class="language-cjs">const url = require('node:url'); const myURL = new URL('https://a:b@測試?abc#foo'); console.log(myURL.href); // Prints https://a:b@xn--g6w251d/?abc#foo console.log(myURL.toString()); // Prints https://a:b@xn--g6w251d/?abc#foo console.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); // Prints 'https://測試/?abc' </code></pre> <h4><code>url.pathToFileURL(path)</code><span><a class="mark" href="#urlpathtofileurlpath" id="urlpathtofileurlpath">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathtofileurl_path"></a></h4> <div class="api_metadata"> <span>Added in: v10.12.0</span> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The path to convert to a File URL.</li> <li>Returns: <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The file URL object.</li> </ul> <p>This function ensures that <code>path</code> is resolved absolutely, and that the URL control characters are correctly encoded when converting into a File URL.</p> <pre><code class="language-mjs">import { pathToFileURL } from 'node:url'; new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) </code></pre> <pre><code class="language-cjs">const { pathToFileURL } = require('node:url'); new URL(__filename); // Incorrect: throws (POSIX) new URL(__filename); // Incorrect: C:\... (Windows) pathToFileURL(__filename); // Correct: file:///... (POSIX) pathToFileURL(__filename); // Correct: file:///C:/... (Windows) new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) </code></pre> <h4><code>url.urlToHttpOptions(url)</code><span><a class="mark" href="#urlurltohttpoptionsurl" id="urlurltohttpoptionsurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_urltohttpoptions_url"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.17.0</td> <td><p>The returned object will also contain all the own enumerable properties of the <code>url</code> argument.</p> </td></tr> <tr><td>v15.7.0, v14.18.0</td> <td><p><span>Added in: v15.7.0, v14.18.0</span></p> </td></tr> </table> </details> </div><ul> <li><code>url</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The <a href="#the-whatwg-url-api">WHATWG URL</a> object to convert to an options object.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Options object<ul> <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Protocol to use.</li> <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A domain name or IP address of the server to issue the request to.</li> <li><code>hash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The fragment portion of the URL.</li> <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The serialized query portion of the URL.</li> <li><code>pathname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The path portion of the URL.</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Request path. Should include query string if any. E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.</li> <li><code>href</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The serialized URL.</li> <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port of remote server.</li> <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Basic authentication i.e. <code>'user:password'</code> to compute an Authorization header.</li> </ul> </li> </ul> <p>This utility function converts a URL object into an ordinary options object as expected by the <a href="http.html#httprequestoptions-callback"><code>http.request()</code></a> and <a href="https.html#httpsrequestoptions-callback"><code>https.request()</code></a> APIs.</p> <pre><code class="language-mjs">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' } */ </code></pre> <pre><code class="language-cjs">const { urlToHttpOptions } = require('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' } */ </code></pre> </section><section><h3>Legacy URL API<span><a class="mark" href="#legacy-url-api" id="legacy-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_url_api"></a></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.13.0, v14.17.0</td> <td><p>Deprecation revoked. Status changed to "Legacy".</p> </td></tr> <tr><td>v11.0.0</td> <td><p>This API is deprecated.</p> </td></tr> </table> </details> </div><blockquote> <p>Stability: 3 - Legacy: Use the WHATWG URL API instead.</p> </blockquote> <h4>Legacy <code>urlObject</code><span><a class="mark" href="#legacy-urlobject" id="legacy-urlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_urlobject"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.13.0, v14.17.0</td> <td><p>Deprecation revoked. Status changed to "Legacy".</p> </td></tr> <tr><td>v11.0.0</td> <td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p> </td></tr> </table> </details> </div><blockquote> <p>Stability: 3 - Legacy: Use the WHATWG URL API instead.</p> </blockquote> <p>The legacy <code>urlObject</code> (<code>require('node:url').Url</code> or <code>import { Url } from 'node:url'</code>) is created and returned by the <code>url.parse()</code> function.</p> <h5><code>urlObject.auth</code><span><a class="mark" href="#urlobjectauth" id="urlobjectauth">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_auth"></a></h5> <p>The <code>auth</code> property is the username and password portion of the URL, also referred to as <em>userinfo</em>. This string subset follows the <code>protocol</code> and double slashes (if present) and precedes the <code>host</code> component, delimited by <code>@</code>. The string is either the username, or it is the username and password separated by <code>:</code>.</p> <p>For example: <code>'user:pass'</code>.</p> <h5><code>urlObject.hash</code><span><a class="mark" href="#urlobjecthash" id="urlobjecthash">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hash"></a></h5> <p>The <code>hash</code> property is the fragment identifier portion of the URL including the leading <code>#</code> character.</p> <p>For example: <code>'#hash'</code>.</p> <h5><code>urlObject.host</code><span><a class="mark" href="#urlobjecthost" id="urlobjecthost">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_host"></a></h5> <p>The <code>host</code> property is the full lower-cased host portion of the URL, including the <code>port</code> if specified.</p> <p>For example: <code>'sub.example.com:8080'</code>.</p> <h5><code>urlObject.hostname</code><span><a class="mark" href="#urlobjecthostname" id="urlobjecthostname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hostname"></a></h5> <p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code> component <em>without</em> the <code>port</code> included.</p> <p>For example: <code>'sub.example.com'</code>.</p> <h5><code>urlObject.href</code><span><a class="mark" href="#urlobjecthref" id="urlobjecthref">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_href"></a></h5> <p>The <code>href</code> property is the full URL string that was parsed with both the <code>protocol</code> and <code>host</code> components converted to lower-case.</p> <p>For example: <code>'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>.</p> <h5><code>urlObject.path</code><span><a class="mark" href="#urlobjectpath" id="urlobjectpath">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_path"></a></h5> <p>The <code>path</code> property is a concatenation of the <code>pathname</code> and <code>search</code> components.</p> <p>For example: <code>'/p/a/t/h?query=string'</code>.</p> <p>No decoding of the <code>path</code> is performed.</p> <h5><code>urlObject.pathname</code><span><a class="mark" href="#urlobjectpathname" id="urlobjectpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_pathname"></a></h5> <p>The <code>pathname</code> property consists of the entire path section of the URL. This is everything following the <code>host</code> (including the <code>port</code>) and before the start of the <code>query</code> or <code>hash</code> components, delimited by either the ASCII question mark (<code>?</code>) or hash (<code>#</code>) characters.</p> <p>For example: <code>'/p/a/t/h'</code>.</p> <p>No decoding of the path string is performed.</p> <h5><code>urlObject.port</code><span><a class="mark" href="#urlobjectport" id="urlobjectport">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_port"></a></h5> <p>The <code>port</code> property is the numeric port portion of the <code>host</code> component.</p> <p>For example: <code>'8080'</code>.</p> <h5><code>urlObject.protocol</code><span><a class="mark" href="#urlobjectprotocol" id="urlobjectprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_protocol"></a></h5> <p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p> <p>For example: <code>'http:'</code>.</p> <h5><code>urlObject.query</code><span><a class="mark" href="#urlobjectquery" id="urlobjectquery">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_query"></a></h5> <p>The <code>query</code> property is either the query string without the leading ASCII question mark (<code>?</code>), or an object returned by the <a href="querystring.html"><code>querystring</code></a> module's <code>parse()</code> method. Whether the <code>query</code> property is a string or object is determined by the <code>parseQueryString</code> argument passed to <code>url.parse()</code>.</p> <p>For example: <code>'query=string'</code> or <code>{'query': 'string'}</code>.</p> <p>If returned as a string, no decoding of the query string is performed. If returned as an object, both keys and values are decoded.</p> <h5><code>urlObject.search</code><span><a class="mark" href="#urlobjectsearch" id="urlobjectsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_search"></a></h5> <p>The <code>search</code> property consists of the entire "query string" portion of the URL, including the leading ASCII question mark (<code>?</code>) character.</p> <p>For example: <code>'?query=string'</code>.</p> <p>No decoding of the query string is performed.</p> <h5><code>urlObject.slashes</code><span><a class="mark" href="#urlobjectslashes" id="urlobjectslashes">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_slashes"></a></h5> <p>The <code>slashes</code> property is a <code>boolean</code> with a value of <code>true</code> if two ASCII forward-slash characters (<code>/</code>) are required following the colon in the <code>protocol</code>.</p> <h4><code>url.format(urlObject)</code><span><a class="mark" href="#urlformaturlobject" id="urlformaturlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_urlobject"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v17.0.0</td> <td><p>Now throws an <code>ERR_INVALID_URL</code> exception when Punycode conversion of a hostname introduces changes that could cause the URL to be re-parsed differently.</p> </td></tr> <tr><td>v15.13.0, v14.17.0</td> <td><p>Deprecation revoked. Status changed to "Legacy".</p> </td></tr> <tr><td>v11.0.0</td> <td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>URLs with a <code>file:</code> scheme will now always use the correct number of slashes regardless of <code>slashes</code> option. A falsy <code>slashes</code> option with no protocol is now also respected at all times.</p> </td></tr> <tr><td>v0.1.25</td> <td><p><span>Added in: v0.1.25</span></p> </td></tr> </table> </details> </div><blockquote> <p>Stability: 3 - Legacy: Use the WHATWG URL API instead.</p> </blockquote> <ul> <li><code>urlObject</code> {Object|string} A URL object (as returned by <code>url.parse()</code> or constructed otherwise). If a string, it is converted to an object by passing it to <code>url.parse()</code>.</li> </ul> <p>The <code>url.format()</code> method returns a formatted URL string derived from <code>urlObject</code>.</p> <pre><code class="language-js">const url = require('node:url'); url.format({ protocol: 'https', hostname: 'example.com', pathname: '/some/path', query: { page: 1, format: 'json', }, }); // => 'https://example.com/some/path?page=1&format=json' </code></pre> <p>If <code>urlObject</code> is not an object or a string, <code>url.format()</code> will throw a <a href="errors.html#class-typeerror"><code>TypeError</code></a>.</p> <p>The formatting process operates as follows:</p> <ul> <li>A new empty string <code>result</code> is created.</li> <li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</li> <li>Otherwise, if <code>urlObject.protocol</code> is not <code>undefined</code> and is not a string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> <li>For all string values of <code>urlObject.protocol</code> that <em>do not end</em> with an ASCII colon (<code>:</code>) character, the literal string <code>:</code> will be appended to <code>result</code>.</li> <li>If either of the following conditions is true, then the literal string <code>//</code> will be appended to <code>result</code>:<ul> <li><code>urlObject.slashes</code> property is true;</li> <li><code>urlObject.protocol</code> begins with <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, or <code>file</code>;</li> </ul> </li> <li>If the value of the <code>urlObject.auth</code> property is truthy, and either <code>urlObject.host</code> or <code>urlObject.hostname</code> are not <code>undefined</code>, the value of <code>urlObject.auth</code> will be coerced into a string and appended to <code>result</code> followed by the literal string <code>@</code>.</li> <li>If the <code>urlObject.host</code> property is <code>undefined</code> then:<ul> <li>If the <code>urlObject.hostname</code> is a string, it is appended to <code>result</code>.</li> <li>Otherwise, if <code>urlObject.hostname</code> is not <code>undefined</code> and is not a string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> <li>If the <code>urlObject.port</code> property value is truthy, and <code>urlObject.hostname</code> is not <code>undefined</code>:<ul> <li>The literal string <code>:</code> is appended to <code>result</code>, and</li> <li>The value of <code>urlObject.port</code> is coerced to a string and appended to <code>result</code>.</li> </ul> </li> </ul> </li> <li>Otherwise, if the <code>urlObject.host</code> property value is truthy, the value of <code>urlObject.host</code> is coerced to a string and appended to <code>result</code>.</li> <li>If the <code>urlObject.pathname</code> property is a string that is not an empty string:<ul> <li>If the <code>urlObject.pathname</code> <em>does not start</em> with an ASCII forward slash (<code>/</code>), then the literal string <code>'/'</code> is appended to <code>result</code>.</li> <li>The value of <code>urlObject.pathname</code> is appended to <code>result</code>.</li> </ul> </li> <li>Otherwise, if <code>urlObject.pathname</code> is not <code>undefined</code> and is not a string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> <li>If the <code>urlObject.search</code> property is <code>undefined</code> and if the <code>urlObject.query</code> property is an <code>Object</code>, the literal string <code>?</code> is appended to <code>result</code> followed by the output of calling the <a href="querystring.html"><code>querystring</code></a> module's <code>stringify()</code> method passing the value of <code>urlObject.query</code>.</li> <li>Otherwise, if <code>urlObject.search</code> is a string:<ul> <li>If the value of <code>urlObject.search</code> <em>does not start</em> with the ASCII question mark (<code>?</code>) character, the literal string <code>?</code> is appended to <code>result</code>.</li> <li>The value of <code>urlObject.search</code> is appended to <code>result</code>.</li> </ul> </li> <li>Otherwise, if <code>urlObject.search</code> is not <code>undefined</code> and is not a string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> <li>If the <code>urlObject.hash</code> property is a string:<ul> <li>If the value of <code>urlObject.hash</code> <em>does not start</em> with the ASCII hash (<code>#</code>) character, the literal string <code>#</code> is appended to <code>result</code>.</li> <li>The value of <code>urlObject.hash</code> is appended to <code>result</code>.</li> </ul> </li> <li>Otherwise, if the <code>urlObject.hash</code> property is not <code>undefined</code> and is not a string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> <li><code>result</code> is returned.</li> </ul> <h4><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code><span><a class="mark" href="#urlparseurlstring-parsequerystring-slashesdenotehost" id="urlparseurlstring-parsequerystring-slashesdenotehost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_parse_urlstring_parsequerystring_slashesdenotehost"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.13.0</td> <td><p>Documentation-only deprecation.</p> </td></tr> <tr><td>v15.13.0, v14.17.0</td> <td><p>Deprecation revoked. Status changed to "Legacy".</p> </td></tr> <tr><td>v11.14.0</td> <td><p>The <code>pathname</code> property on the returned URL object is now <code>/</code> when there is no path and the protocol scheme is <code>ws:</code> or <code>wss:</code>.</p> </td></tr> <tr><td>v11.0.0</td> <td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p> </td></tr> <tr><td>v9.0.0</td> <td><p>The <code>search</code> property on the returned URL object is now <code>null</code> when no query string is present.</p> </td></tr> <tr><td>v0.1.25</td> <td><p><span>Added in: v0.1.25</span></p> </td></tr> </table> </details> </div><blockquote> <p>Stability: 0 - Deprecated: Use the WHATWG URL API instead.</p> </blockquote> <ul> <li><code>urlString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The URL string to parse.</li> <li><code>parseQueryString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the <code>query</code> property will always be set to an object returned by the <a href="querystring.html"><code>querystring</code></a> module's <code>parse()</code> method. If <code>false</code>, the <code>query</code> property on the returned URL object will be an unparsed, undecoded string. <strong>Default:</strong> <code>false</code>.</li> <li><code>slashesDenoteHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the first token after the literal string <code>//</code> and preceding the next <code>/</code> will be interpreted as the <code>host</code>. For instance, given <code>//foo/bar</code>, the result would be <code>{host: 'foo', pathname: '/bar'}</code> rather than <code>{pathname: '//foo/bar'}</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> <p>The <code>url.parse()</code> method takes a URL string, parses it, and returns a URL object.</p> <p>A <code>TypeError</code> is thrown if <code>urlString</code> is not a string.</p> <p>A <code>URIError</code> is thrown if the <code>auth</code> property is present but cannot be decoded.</p> <p><code>url.parse()</code> uses a lenient, non-standard algorithm for parsing URL strings. It is prone to security issues such as <a href="https://hackerone.com/reports/678487">host name spoofing</a> and incorrect handling of usernames and passwords. Do not use with untrusted input. CVEs are not issued for <code>url.parse()</code> vulnerabilities. Use the <a href="#the-whatwg-url-api">WHATWG URL</a> API instead.</p> <h4><code>url.resolve(from, to)</code><span><a class="mark" href="#urlresolvefrom-to" id="urlresolvefrom-to">#</a></span><a aria-hidden="true" class="legacy" id="url_url_resolve_from_to"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.13.0, v14.17.0</td> <td><p>Deprecation revoked. Status changed to "Legacy".</p> </td></tr> <tr><td>v11.0.0</td> <td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p> </td></tr> <tr><td>v6.6.0</td> <td><p>The <code>auth</code> fields are now kept intact when <code>from</code> and <code>to</code> refer to the same host.</p> </td></tr> <tr><td>v6.0.0</td> <td><p>The <code>auth</code> fields is cleared now the <code>to</code> parameter contains a hostname.</p> </td></tr> <tr><td>v6.5.0, v4.6.2</td> <td><p>The <code>port</code> field is copied correctly now.</p> </td></tr> <tr><td>v0.1.25</td> <td><p><span>Added in: v0.1.25</span></p> </td></tr> </table> </details> </div><blockquote> <p>Stability: 3 - Legacy: Use the WHATWG URL API instead.</p> </blockquote> <ul> <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to use if <code>to</code> is a relative URL.</li> <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The target URL to resolve.</li> </ul> <p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a manner similar to that of a web browser resolving an anchor tag.</p> <pre><code class="language-js">const url = require('node:url'); url.resolve('/one/two/three', 'four'); // '/one/two/four' url.resolve('http://example.com/', '/one'); // 'http://example.com/one' url.resolve('http://example.com/one', '/two'); // 'http://example.com/two' </code></pre> <p>To achieve the same result using the WHATWG URL API:</p> <pre><code class="language-js">function resolve(from, to) { const resolvedUrl = new URL(to, new URL(from, 'resolve://')); if (resolvedUrl.protocol === 'resolve:') { // `from` is a relative URL. const { pathname, search, hash } = resolvedUrl; return pathname + search + hash; } return resolvedUrl.toString(); } resolve('/one/two/three', 'four'); // '/one/two/four' resolve('http://example.com/', '/one'); // 'http://example.com/one' resolve('http://example.com/one', '/two'); // 'http://example.com/two' </code></pre> <p><a id="whatwg-percent-encoding"></a></p> </section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#percent-encoding-in-urls" id="percent-encoding-in-urls">#</a></span><a aria-hidden="true" class="legacy" id="url_percent_encoding_in_urls"></a></h3> <p>URLs are permitted to only contain a certain range of characters. Any character falling outside of that range must be encoded. How such characters are encoded, and which characters to encode depends entirely on where the character is located within the structure of the URL.</p> <h4>Legacy API<span><a class="mark" href="#legacy-api" id="legacy-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_api"></a></h4> <p>Within the Legacy API, spaces (<code>' '</code>) and the following characters will be automatically escaped in the properties of URL objects:</p> <pre><code class="language-text">< > " ` \r \n \t { } | \ ^ ' </code></pre> <p>For example, the ASCII space character (<code>' '</code>) is encoded as <code>%20</code>. The ASCII forward slash (<code>/</code>) character is encoded as <code>%3C</code>.</p> <h4>WHATWG API<span><a class="mark" href="#whatwg-api" id="whatwg-api">#</a></span><a aria-hidden="true" class="legacy" id="url_whatwg_api"></a></h4> <p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> uses a more selective and fine grained approach to selecting encoded characters than that used by the Legacy API.</p> <p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges of characters that must be percent-encoded:</p> <ul> <li><p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to U+001F (inclusive) and all code points greater than U+007E (~).</p> </li> <li><p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em> and code points U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>), and U+0060 (`).</p> </li> <li><p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em> and code points U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), U+003E (>), U+003F (?), U+0060 (`), U+007B ({), and U+007D (}).</p> </li> <li><p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code points U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@), U+005B ([) to U+005E(^), and U+007C (|).</p> </li> </ul> <p>The <em>userinfo percent-encode set</em> is used exclusively for username and passwords encoded within the URL. The <em>path percent-encode set</em> is used for the path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments. The <em>C0 control percent-encode set</em> is used for host and path under certain specific conditions, in addition to all other cases.</p> <p>When non-ASCII characters appear within a host name, the host name is encoded using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm. Note, however, that a host name <em>may</em> contain <em>both</em> Punycode encoded and percent-encoded characters:</p> <pre><code class="language-js">const myURL = new URL('https://%CF%80.example.com/foo'); console.log(myURL.href); // Prints https://xn--1xa.example.com/foo console.log(myURL.origin); // Prints https://xn--1xa.example.com </code></pre> </section> <!-- API END --> </div> </div> </div> </body> </html>