URL網址解析
[root@localhost ~]# node
> url
{ parse: [Function: urlParse],
resolve: [Function: urlResolve],
resolveObject: [Function: urlResolveObject],
format: [Function: urlFormat],
Url: [Function: Url] }
parse
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
- urlString:<String> The URL string to parse.
- 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. Defaults to 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'}. Defaults to false.
【實例】
> url.parse('https://www.google.com/?gws_rd=ssl')
Url {
protocol: 'https:',//協議
slashes: true, //是否有協議的雙斜線
auth: null,
host: 'www.google.com', //ip地址,域名
port: null, //端口
hostname: 'www.google.com', //主機名
hash: null, //錨點
search: '?gws_rd=ssl', //查詢字符串參數
query: 'gws_rd=ssl', //發送給服務器的數據
pathname: '/', //訪問資源的路徑名
path: '/?gws_rd=ssl', //路徑
href: 'https://www.google.com/?gws_rd=ssl' }
【第二個參數實例演示】隨便寫的域名
> url.parse('https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1')
# 輸出結果
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.zhangdanyang.com:80',
port: '80',
hostname: 'www.zhangdanyang.com',
hash: '#floor1',
search: '?from=zdy&search=hello',
query: 'from=zdy&search=hello',
pathname: '/article/list',
path: '/article/list?from=zdy&search=hello',
href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' }
> url.parse('https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1',true)
# 輸出結果
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.zhangdanyang.com:80',
port: '80',
hostname: 'www.zhangdanyang.com',
hash: '#floor1',
search: '?from=zdy&search=hello',
query: { from: 'zdy', search: 'hello' },//第二個參數為true時,將query解析為對象
pathname: '/article/list',
path: '/article/list?from=zdy&search=hello',
href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' }
【第三個參數實例演示】
> url.parse('//www.google.com/',true)
# 輸出結果
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '',
query: {},
pathname: '//www.google.com/',
path: '//www.google.com/',
href: '//www.google.com/' }
> url.parse('//www.google.com/',true,true)
# 輸出結果
Url {
protocol: null,
slashes: true,
auth: null,
host: 'www.google.com',
port: null,
hostname: 'www.google.com',
hash: null,
search: '',
query: {},
pathname: '/',
path: '/',
href: '//www.google.com/' }
format
url.format(urlObject)
【實例】
> url.format({
... protocol: 'https:',
... slashes: true,
... auth: null,
... host: 'www.zhangdanyang.com:80',
... port: '80',
... hostname: 'www.zhangdanyang.com',
... hash: '#floor1',
... search: '?from=zdy&search=hello',
... query: 'from=zdy&search=hello',
... pathname: '/article/list',
... path: '/article/list?from=zdy&search=hello',
... href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' })
# 輸出結果
'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1'
resolve
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'