Node中的url模块和querystring模块怎么使用(node,querystring,URL,web开发)

时间:2024-05-02 09:53:52 作者 : 石家庄SEO 分类 : web开发
  • TAG :

url模块和querystring模块是非常重要的两个URL处理模块。在做node服务端的开发时会经常用到。

url

在介绍url模块之前我们先来一张图,看懂了这张图对于url这个模块你就基本上没什么问题了。

Node中的url模块和querystring模块怎么使用

我们来解释下各自的含义

  • 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:原始的地址。不过需要注意的是,protocolhost会被转成小写字母。

下面我们来讲解下它的三个常用方法

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);

输出

Node中的url模块和querystring模块怎么使用

该方法还支持传递另外两个参数,parseQueryStringslashesDenoteHos

parseQueryString:(默认为false)如为false,则urlObject.query为未解析的字符串,比如nick=%E4%B8%AD%E6%96%87,且对应的值不会decode;如果parseQueryString为true,则urlObject.queryobject,比如{ 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);

Node中的url模块和querystring模块怎么使用

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);

Node中的url模块和querystring模块怎么使用

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查询参数的解析。这里我们重点分析下它的parsestringify两个方法。

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模块怎么使用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:php没有定时功能的原因是什么下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18