url.parse(urlString[, parseQueryString[, slashesDenoteHost]])


稳定性: 3 - 旧版:请改用 WHATWG URL API。

¥Stability: 3 - Legacy: Use the WHATWG URL API instead.

  • urlString <string> 要解析的 URL 字符串。

    ¥urlString <string> The URL string to parse.

  • parseQueryString <boolean> 如果为 true,则 query 属性将始终设置为 querystring 模块的 parse() 方法返回的对象。如果为 false,则返回的网址对象上的 query 属性将是未解析、未解码的字符串。默认值:false

    ¥parseQueryString <boolean> If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. Default: false.

  • slashesDenoteHost <boolean> 如果为 true,则字面串 // 之后和下一个 / 之前的第一个令牌将被解释为 host。例如,给定 //foo/bar,结果将是 {host: 'foo', pathname: '/bar'} 而不是 {pathname: '//foo/bar'}。默认值:false

    ¥slashesDenoteHost <boolean> If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. Default: false.

url.parse() 方法接受网址字符串,解析并返回网址对象。

¥The url.parse() method takes a URL string, parses it, and returns a URL object.

如果 urlString 不是字符串,则抛出 TypeError

¥A TypeError is thrown if urlString is not a string.

如果 auth 属性存在但无法解码,则抛出 URIError

¥A URIError is thrown if the auth property is present but cannot be decoded.

url.parse() 使用一种宽松的、非标准的算法来解析 URL 字符串。它很容易出现安全问题,例如 主机名欺骗 和用户名和密码的不正确处理。

¥url.parse() uses a lenient, non-standard algorithm for parsing URL strings. It is prone to security issues such as host name spoofing and incorrect handling of usernames and passwords.

url.parse() 是大多数旧版 API 的例外。尽管存在安全问题,但它是旧版的并且没有被弃用,因为它是:

¥url.parse() is an exception to most of the legacy APIs. Despite its security concerns, it is legacy and not deprecated because it is:

  • 比替代的 WHATWG URL 解析器更快。

    ¥Faster than the alternative WHATWG URL parser.

  • 与替代的 WHATWG URL API 相比,相对 URL 更易于使用。

    ¥Easier to use with regards to relative URLs than the alternative WHATWG URL API.

  • 在 npm 生态系统中广泛依赖。

    ¥Widely relied upon within the npm ecosystem.

谨慎使用。

¥Use with caution.