使用node.JS中的url模块解析URL信息
在HTTP部分,详细介绍了URL的相关知识。而nodejs中的url模块提供了一些实用函数,用于URL处理与解析。
解析URL
解析URL对象有以下内容,依赖于他们是否在URL字符串里存在。任何不在URL字符串里的部分,都不会出现在解析对象里
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
┌─────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤
│protocol││ auth │ host │ path │hash │
│ ││ ├──────────┬──────┼──────────┬────────────────┤ │
│ ││ │hostname│port│pathname│ search │ │
│ ││ │ │ │ ├─┬──────────────┤ │
│ ││ │ │ │ ││ query │ │
" http: //user:pass@host.com:8080 /p/a/t/h ? query=string #hash"
│ ││ │ │ │ ││ │ │
└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘
href:准备解析的完整的URL,包含协议和主机(小写)
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
protocol:请求协议,小写
'http:'
slashes:协议要求的斜杠(冒号后)
true或false
host:完整的URL小写主机部分,包含端口信息
'host.com:8080'
auth:url中的验证信息
'user:pass'
hostname:域名中的小写主机名
'host.com'
port:主机的端口号
'8080'
pathname:URL中的路径部分,在主机名后,查询字符前,包含第一个斜杠
'/p/a/t/h'
search:URL中的查询字符串,包含开头的问号
'?query=string'
path:pathname和search连在一起
'/p/a/t/h?query=string'
query:查询字符串中得参数部分,或者使用querystring.parse()解析后返回的对象
'query=string'or{'query':'string'}
hash:URL的“#”后面部分(包括#符号)
'#hash'
URL方法
URL模块包含分析和解析URL的工具。调用require('url')来访问模块
varurl=require('url');
/*
{parse:[Function:urlParse],
resolve:[Function:urlResolve],
resolveObject:[Function:urlResolveObject],
format:[Function:urlFormat],
Url:[Function:Url]}
*/
console.log(url);
url.parse(urlStr[,parseQueryString][,slashesDenoteHost])
输入URL字符串,返回一个对象
第二个参数parseQueryString(默认为false),如为false,则urlObject.query为未解析的字符串,比如author=%E5%B0%8F%E7%81%AB%E6%9F%B4,且对应的值不会decode;如果parseQueryString为true,则urlObject.query为object,比如{author:'小火柴'},且值会被decode
第三个参数slashesDenoteHos(默认为false),如果为true,可以正确解析不带协议头的URL,类似//foo/bar里的foo就会被认为是hostname;如果为false,则foo被认为是pathname的一部分
varurl=require('url');
varstr='http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash';
/*
Url{
protocol:'http:',
slashes:true,
auth:'user:pass',
host:'host.com:8080',
port:'8080',
hostname:'host.com',
hash:'#hash',
search:'?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
query:'author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
pathname:'/p/a/t/h',
path:'/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
href:'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash'}
*/
console.log(url.parse(str));
varurl=require('url');
varstr='http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash';
/*
Url{
protocol:'http:',
slashes:true,
auth:'user:pass',
host:'host.com:8080',
port:'8080',
hostname:'host.com',
hash:'#hash',
search:'?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
query:{author:'小火柴'},
pathname:'/p/a/t/h',
path:'/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
href:'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash'}
*/
console.log(url.parse(str,true));
varurl=require('url');
varstr='//foo/bar';
varresult1=url.parse(str,true);
varresult2=url.parse(str,true,true);
console.log(result1.path);//'//foo/bar'
console.log(result1.pathname);//'//foo/bar'
console.log(result1.hostname);//null
console.log(result2.path);//'/bar'
console.log(result2.pathname);//'/bar'
console.log(result2.hostname);//'foo'
url.format(urlObject)
url.parse(str)的反向操作,输入一个解析过的URL对象,返回格式化过的字符串
urlObject包含了很多字段,比如protocol、slashes、protocol等,且不一定需要全部传,所以有一套解析逻辑
格式化的工作流程如下
href会被忽略
protocol无论是否有末尾的:(冒号),会同样的处理
http,https,ftp,gopher,file协议会被添加后缀://
mailto,xmpp,aim,sftp,foo,等协议添加后缀:
slashes如果协议需要://,设置为true
仅需对之前列出的没有斜杠的协议,比如议mongodb://localhost:8000/
auth如果出现将会使用.
hostname仅在缺少host时使用
port仅在缺少host时使用
host用来替换hostname和port
pathname无论结尾是否有/将会同样处理
search将会替代query属性
无论前面是否有/将会同样处理
query(对象;参见querystring)如果没有search,将会使用
hash无论前面是否有#,都会同样处理
varurl=require('url');
varobj={
protocol:'http:',
auth:'user:pass',
host:'host.com:8080',
hash:'#hash',
query:{author:'小火柴'}
}
//http://user:pass@host.com:8080?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash
console.log(url.format(obj));
url.resolve(from,to)
url.resolve()方法以一种浏览器解析超链接的方式把一个目标URL解析成相对于一个基础URL,参数如下
from
解析时相对的基本URL。 to
要解析的超链接URL。
varurl=require('url');
console.log(url.resolve('/one/two/three','four'));//'/one/two/four'
console.log(url.resolve('http://example.com/','/one'));//'http://example.com/one'
console.log(url.resolve('http://example.com/one','/two'));//'http://example.com/two'
更多关于node.JS中url模块的使用方法大家可参考下面的相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。