Node.js Manual & Documentation


Table Of Contents


URL URL模块

This module has utilities for URL resolution and parsing. Call require('url') to use it.

此模块包含用于解析和分析URL的工具。可通过require('url')访问他们。

Parsed URL objects have some or all of the following fields, depending on whether or not they exist in the URL string. Any parts that are not in the URL string will not be in the parsed object. Examples are shown for the URL

解析后的URL对象包含下述部分或全部字段。具体包含哪些字段取决于解析前的URL字符串中是否存在这些字段。在原始的URL字符串中不存在的字段在解析后的对象中也不会包含。以下面这个URL为例:

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

The following methods are provided by the URL module:

URL模块提供了如下方法:

url.parse(urlStr, parseQueryString=false)

Take a URL string, and return an object. Pass true as the second argument to also parse the query string using the querystring module.

以一个 URL字符串为参数,返回一个解析后的对象。如设置第二个参数为true,则会使用querystring模块解析URL中的查询字符串。

url.format(urlObj)

Take a parsed URL object, and return a formatted URL string.

以一个解析后的URL对象为参数,返回格式化的URL字符串。

url.resolve(from, to)

Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.

指定一个默认URL地址,和一个链接的目标URL地址,返回链接的绝对URL地址。处理方式与浏览器处理锚点标签的方法一致。