Node中的url模块和querystring模块怎么使用
导读:本文共4160字符,通常情况下阅读需要14分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: url模块和querystring模块是非常重要的两个URL处理模块。在做node服务端的开发时会经常用到。url在介绍url模块之前我们先来一张图,看懂了这张图对于url这个模块你就基本上没什么问题了。我们来解释下各自的含义protocol:协议,... ...
目录
(为您整理了一些要点),点击可以直达。
url
模块和querystring
模块是非常重要的两个URL
处理模块。在做node
服务端的开发时会经常用到。
url
在介绍url
模块之前我们先来一张图,看懂了这张图对于url
这个模块你就基本上没什么问题了。
我们来解释下各自的含义
protocol:协议,需要注意的是包含了
:
,并且是小写的。【相关教程推荐:nodejs视频教程、编程教学】slashes:如果
:
后面跟了两个//
,那么为true。auth:认证信息,如果有密码,为
usrname:passwd
,如果没有,则为usrname
。注意,这里区分大小写。host:主机名。注意包含了端口,比如
ke.qq.com:8080
,并且是小写的。hostname:主机名,不包含端口,并且是小写的。
port: 端口号。
path:路径部分,包含search部分。
pathname:路径部分,不包含search部分。
search:查询字符串,注意,包含了
?
,此外,值是没有经过decode的。query:字符串 或者 对象。如果是字符串,则是
search
去掉?
,其余一样;如果是对象,那么是decode过的。hash:哈希部分,注意包含了
#
。href:原始的地址。不过需要注意的是,
protocol
、host
会被转成小写字母。
下面我们来讲解下它的三个常用方法
parse(urlString, parseQueryString, slashesDenoteHost)
该方法将url
字符串,解析成object
,便于开发者进行操作。
consturl=require("url");conststr="http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1";constobj=url.parse(str);console.log(obj);
输出
该方法还支持传递另外两个参数,parseQueryString
和slashesDenoteHos
parseQueryString:(默认为false)如为false
,则urlObject.query
为未解析的字符串,比如nick=%E4%B8%AD%E6%96%87
,且对应的值不会decode
;如果parseQueryString
为true,则urlObject.query
为object
,比如{ nick: '中文' }
,且值会被`decode;
consturl=require("url");conststr="http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1";constobj2=url.parse(str,true);console.log(obj2);
slashesDenoteHos:(默认为false)如果为true
,那么类似//randy/nick
里的randy
就会被认为是hostname
;如果为false
,则randy
被认为是pathname
的一部分。
光看起来可能不太理解这句话的含义,下面笔者举个例子我相信你们就明白了。
conststr2="//randy/nick";constobj3=url.parse(str2,true,false);console.log(obj3);constobj4=url.parse(str2,true,true);console.log(obj4);
format(urlObject)
这个方法就是parse
的反向操作。将对象转成url
字符串。
constpathObj={protocol:"http:",slashes:true,auth:"user:password",host:"randy.com:8080",port:"8080",hostname:"randy.com",hash:"#part=1",search:"?nick=%E4%B8%AD%E6%96%87",query:"nick=%E4%B8%AD%E6%96%87",pathname:"/index.html",path:"/index.html?nick=%E4%B8%AD%E6%96%87",href:"http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1",};console.log(url.format(pathObj));//http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1
resolve(from, to)
该方法用于解析相对于基本URL
的目标URL
。
console.log(url.resolve("/one/two/three","four"));///one/two/fourconsole.log(url.resolve("http://example.com/","/one"));//http://example.com/oneconsole.log(url.resolve("http://example.com/one","/two"));//http://example.com/twoconsole.log(url.resolve("http://example.com/one/ddd/ddd/ddd","./two"));//http://example.com/one/ddd/ddd/twoconsole.log(url.resolve("http://example.com/one/ddd/ddd/ddd","../two"));//http://example.com/one/ddd/twoconsole.log(url.resolve("http://example.com/one/ddd/ddd/ddd",".../two"));//http://example.com/one/ddd/ddd/.../two
querystring
querystring
这个模块,也是用来做url
查询参数的解析。这里我们重点分析下它的parse
和stringify
两个方法。
parse(str, sep, eq, options)
parse
是将查询字符串转成对象类型,并且也会decode
。
constquerystring=require("querystring");conststr="nick=randy&age=24&nick2=%E4%B8%AD%E6%96%87";constobj=querystring.parse(str);console.log(obj);//{nick:'randy',age:'24',nick2:'中文'}
下面我们再来看看它的第二和第三个参数。其实相当于可以替换&、=
为自定义字符,下面笔者举个例子就很快明白了。
conststr1="name-randy|country-cn";constobj1=querystring.parse(str1);console.log(obj1);//{'name-randy|country-cn':''}constobj2=querystring.parse(str1,"|","-");console.log(obj2);//{name:'randy',country:'cn'}
相当于把&
替换成了|
,把=
替换成了-
。笔者感觉配到这种情况应该不多。
stringify(obj, sep, eq, options)
这个方法就是上面parse
的反向操作。下面咱们直接上例子
constobj3={nick:"randy",age:"24",};conststr4=querystring.stringify(obj3);console.log(str4);//nick=randy&age=24
这个方法也是支持自定义分割符的。
constobj5={name:"randy",country:"cn",};conststr6=querystring.stringify(obj5,"|","-");console.log(str6);//name-randy|country-c
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Node中的url模块和querystring模块怎么使用的详细内容,希望对您有所帮助,信息来源于网络。