Node.js URL模块

The url module provides utilities for URL resolution and parsing.

url模块提供了一个URL地址解析和转换的实用工具。
在node命令行模式被默认引入,但在node脚本中需要显示引入

const url = require('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.

一个URL字符串是一个结构化的字符,包含了多个有意义的成分。当被转化时,将会返回一个URL对象,他的属性是每一个URL组件。

Node.js URL模块_第1张图片

  • URL
    • URL Strings and URL Objects
      • urlObject.href
      • urlObject.protocol
      • urlObject.slashes
      • urlObject.host
      • urlObject.auth
      • urlObject.hostname
      • urlObject.port
      • urlObject.pathname
      • urlObject.search
      • urlObject.path
      • urlObject.query
      • urlObject.hash
    • url.format(urlObject)
    • url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
    • url.resolve(from, to)

href

The href property is the full URL string that was parsed with both the protocol and host components converted to lower-case.

href熟悉是一个完整的被解析为协议和主机信息的小写的URL字符串。

protocol

The protocol property identifies the URL’s lower-cased protocol scheme.

protocol属性定义了URL的小写的通信协议方案。

slashes

The slashes property is a boolean with a value of true if two ASCII forward-slash characters (/) are required following the colon in the protocol.

slashes属性是一个布尔值,如果协议后的冒号后面需要(或存在)两个斜杠,则该值为真。

host

The host property is the full lower-cased host portion of the URL, including the port if specified.

host熟悉是全部由小写字符组成的URL的主机信息部分,如果有指明端口的话就包括端口部分。

auth

The auth property is the username and password portion of the URL, also referred to as “userinfo”. This string subset follows the protocol and double slashes (if present) and precedes the host component, delimited by an ASCII “at sign” (@). The format of the string is {username}[:{password}], with the [:{password}] portion being optional.

auth属性是组成URL的用户名和密码部分,也被称为用户信息。它是由协议字段加两个斜杠(如果有的话)和用ASCII码(@)分割的主机字段中间的部分的子集。该格式化的字符串形式是{usrename}[:{password}],并且[:{password}]部分是可选的。

hostname

The hostname property is the lower-cased host name portion of the host component without the port included.

hostname属性是由小写字符组成的主机组件的主机名部分,不包含端口信息。

port

The port property is the numeric port portion of the host component.

port熟悉是主机组件中的一个数字化的端口部分。

pathname

The pathname property consists of the entire path section of the URL. This is everything following the host (including the port) and before the start of the query or hash components, delimited by either the ASCII question mark (?) or hash (#) characters.
For example ‘/p/a/t/h’
No decoding of the path string is performed.

pathname参数包含了URL完整的路径部分。它们在包括端口的主机字段后面,在用ASCII码(?)分割的查询或用ASCII码(#)分割的井号组件前面。
path字符串是未经解码的。

The search property consists of the entire “query string” portion of the URL, including the leading ASCII question mark (?) character.
For example: ‘?query=string’
No decoding of the query string is performed.

search属性包含了整个组成URL的查询字符串,包括前导的ASCII字符(?)。
注意:查询字符串是未经解码的字符串

path

The path property is a concatenation of the pathname and search components.

path属性是一系列相互关联的路径名和查询组件组成的。

query

The query property is either the “params” portion of the query string everything except the leading ASCII question mark (?), or an object returned by the querystring module’s parse() method:
For example: ‘query=string’ or {‘query’: ‘string’}
If returned as a string, no decoding of the query string is performed. If returned as an object, both keys and values are decoded.

query属性是查询字符串的参数部分,不包含前导的ASCII码字符(?),或者是一个由querystring模块的parse()方法返回的对象。
如果返回的是字符串,那么它是未经解码的查询字符。如果返回的是一个对象,那么键值对都是已经解码完毕的。

hash

The hash property consists of the “fragment” portion of the URL including the leading ASCII hash (#) character.

hash属性表现为URL的片段,由前导的ASCII字符#组成。
注意:这里的hash是’#’号的意思,不是我们平常说的哈希的意思。
这里的hash表示页面的一个片段,浏览器只会滚动到相应位置,不会重新加载网页。

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])、url.format(urlObject)

parse方法的详解请参看鄙人的另一篇博文: http://blog.csdn.net/chy555chy/article/details/52514503

var urlPath = 'http://user:[email protected]:8080/p/a/t/h?query=string#hash';

var urlObj = url.parse(urlPath);
/*
返回的是一个JSON对象。
Url {
    protocol: 'http',
    slashes: true,
    auth: 'user:pass'
    host: 'host.com:8080',
    port: '8080',
    hostname: 'host.com',
    hash: '#hash',
    search: '?query=string',
    query: 'query=string',
    pathname: '/p/a/t/h',
    path: '/p/a/t/h?query=string',
    herf: 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'
}

url.format(urlObj);
//返回 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'
*/

url.resolve(from, to)

The url.resolve() method resolves a target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.

url.resolve()方法相对于基本URL去解析目标URL,在某种程度上和Web浏览器解析锚链接标签有点类似。

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', '.');
//返回 'http://example.com/'

url.resolve('http://example.com/one', '~');
//返回 'http://example.com/~'

url.resolve('http://example.com/one', '../two');
//返回 'http://example.com/two'

Escaped Characters

URLs are only permitted to contain a certain range of characters. Spaces (’ ‘) and the following characters will be automatically escaped in the properties of URL objects:

URLs只被允许包含一些确定范围内的字符。空字符和如下的字符将自动被封装然后填充到URL对象的属性中:

 < > " ` \r \n \t { } | \ ^ ' 

For example, the ASCII space character (’ ‘) is encoded as %20. The ASCII forward slash (/) character is encoded as %3C.

例如,ASCII码的空字符将会被编码为 %20,ASCII码的正斜杠(/)字符会被编码为 %3c

编码规则补充:

  1. 只有4个符号(’.’、’-‘、’*’、’_’)、字母、数字不用转化直接输出。空格(’ ‘)特殊转化为’+’。
  2. 其它的字符转为%+字符的2位十六进制形式。

    C/C++的实现方式,参看鄙人的这篇博文 http://blog.csdn.net/chy555chy/article/details/51440691

你可能感兴趣的:(Node.js,node.js,url)