URL 字符串和 URL 对象


【URL strings and URL objects】

URL 字符串是一个包含多个有意义组成部分的结构化字符串。解析后,将返回一个 URL 对象,其中包含每个组成部分的属性。

【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.】

node:url 模块提供了两种用于处理 URL 的 API:一种是 Node.js 特有的旧版本 API,另一种是实现了浏览器中使用的相同 WHATWG URL 标准 的新版本 API。

【The node:url module provides two APIs for working with URLs: a legacy API that is Node.js specific, and a newer API that implements the same WHATWG URL Standard used by web browsers.】

下面提供了 WHATWG 和传统 API 之间的比较。在 URL 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash' 上方,显示了由传统的 url.parse() 返回的对象的属性。在它下面显示的是 WHATWG URL 对象的属性。

【A comparison between the WHATWG and legacy APIs is provided below. Above the URL 'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash', properties of an object returned by the legacy url.parse() are shown. Below it are properties of a WHATWG URL object.】

WHATWG URL 的 origin 属性包括 protocolhost,但不包括 usernamepassword

【WHATWG URL's origin property includes protocol and host, but not username or password.】

┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                              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.) 

使用 WHATWG API 解析网址字符串:

【Parsing the URL string using the WHATWG API:】

const myURL =
  new URL('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); 

使用旧版 API 解析 URL 字符串:

【Parsing the URL string using the legacy API:】

import url from 'node:url';
const myURL =
  url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');const url = require('node:url');
const myURL =
  url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');