Node.js v0.10.18 手册 & 文档
目录
URL#
稳定度: 3 - 稳定
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 例子:
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
href: The full URL that was originally parsed. Both the protocol and host are lowercased.href: 所解析的完整原始 URL。协议名和主机名都已转为小写。例如:
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'protocol: The request protocol, lowercased.protocol: 请求协议,小写例如:
'http:'host: The full lowercased host portion of the URL, including port information.host: URL主机名已全部转换成小写, 包括端口信息例如:
'host.com:8080'auth: The authentication information portion of a URL.auth:URL中身份验证信息部分例如:
'user:pass'hostname: Just the lowercased hostname portion of the host.hostname:主机的主机名部分, 已转换成小写例如:
'host.com'port: The port number portion of the host.port: 主机的端口号部分例如:
'8080'pathname: The path section of the URL, that comes after the host and before the query, including the initial slash if present.pathname: URL的路径部分,位于主机名之后请求查询之前, including the initial slash if present.例如:
'/p/a/t/h'search: The 'query string' portion of the URL, including the leading question mark.search: URL 的“查询字符串”部分,包括开头的问号。例如:
'?query=string'path: Concatenation ofpathnameandsearch.path:pathname和search连在一起。例如:
'/p/a/t/h?query=string'query: Either the 'params' portion of the query string, or a querystring-parsed object.query: 查询字符串中的参数部分(问号后面部分字符串),或者使用querystring.parse()解析后返回的对象。例如:
'query=string'or{'query':'string'}hash: The 'fragment' portion of the URL including the pound-sign.hash: URL 的 “#” 后面部分(包括 # 符号)例如:
'#hash'
The following methods are provided by the URL module:
以下是 URL 模块提供的方法:
url.parse(urlStr, [parseQueryString], [slashesDenoteHost])#
Take a URL string, and return an object.
输入 URL 字符串,返回一个对象。
Pass true as the second argument to also parse
the query string using the querystring module.
Defaults to false.
将第二个参数设置为 true 则使用 querystring 模块来解析 URL 中德查询字符串部分,默认为 false。
Pass true as the third argument to treat //foo/bar as
{ host: 'foo', pathname: '/bar' } rather than
{ pathname: '//foo/bar' }. Defaults to false.
将第三个参数设置为 true 来把诸如 //foo/bar 这样的URL解析为
{ host: 'foo', pathname: '/bar' } 而不是
{ pathname: '//foo/bar' }。 默认为 false。
url.format(urlObj)#
Take a parsed URL object, and return a formatted URL string.
输入一个 URL 对象,返回格式化后的 URL 字符串。
hrefwill be ignored.protocolis treated the same with or without the trailing:(colon).- The protocols
http,https,ftp,gopher,filewill be postfixed with://(colon-slash-slash). - All other protocols
mailto,xmpp,aim,sftp,foo, etc will be postfixed with:(colon)
- The protocols
authwill be used if present.hostnamewill only be used ifhostis absent.portwill only be used ifhostis absent.hostwill be used in place ofhostnameandportpathnameis treated the same with or without the leading/(slash)searchwill be used in place ofqueryquery(object; seequerystring) will only be used ifsearchis absent.searchis treated the same with or without the leading?(question mark)hashis treated the same with or without the leading#(pound sign, anchor)href属性会被忽略处理.protocol无论是否有末尾的:(冒号),会同样的处理- 这些协议包括
http,https,ftp,gopher,file后缀是://(冒号-斜杠-斜杠). - 所有其他的协议如
mailto,xmpp,aim,sftp,foo, 等 会加上后缀:(冒号)
- 这些协议包括
auth如果有将会出现.hostname如果host属性没被定义,则会使用此属性.port如果host属性没被定义,则会使用此属性.host优先使用,将会替代hostname和portpathname将会同样处理无论结尾是否有/(斜杠)search将会替代query属性query(object类型; 详细请看querystring) 如果没有search,将会使用此属性.search无论前面是否有?(问号),都会同样的处理hash无论前面是否有#(井号, 锚点),都会同样处理
url.resolve(from, to)#
Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag. Examples:
给定一个基础URL路径,和一个href 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'
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'
