地址
说明
url模块提供了一些工具对请求的url进行解析
url结构
一个请求的url包括什么呢?
example
url.parse('http://user:pass@host.com:8080/p/a/t/h?query=string#hash')
output
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',
href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
}
- 很多的字段都是显而易见的,比如protocol,hostname等。而它们之间的关系也很容易看出来。比如
host=hostname+port
,path=pathname+search
等等。
url.resolve(from, to)
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跳到另外一个url。对于第二个参数来说,如果不是/
开头,表明这是一个相对路径;否则是绝对路径。
小结
嗯,差不多就是这样了。用得比较多的是 parse
方法.