nodeJS---URL相关模块用法(url和querystring)
nodeJS---URL相关模块用法(url和querystring)
一: URL模块:
URL模块用于解析和处理URL的字符串,提供了如下三个方法:
1. parse 2. format 3. resolve
1.1 url.parse(urlString); 将url字符串地址转为一个对象。
如下代码:
const url = require('url'); const urlString = url.parse('http://www.nodejs.org/some/url/?with=query¶m=that#about');console.log(urlString);输出如下: // 输出: Url {protocol: 'http:',slashes: true,auth: null,host: 'www.nodejs.org',port: null,hostname: 'www.nodejs.org',hash: '#about',search: '?with=query¶m=that',query: 'with=query¶m=that',pathname: '/some/url/',path: '/some/url/?with=query¶m=that',href: 'http://www.nodejs.org/some/url/?with=query¶m=that#about' }
protocal: url协议
auth: 用户认证
host: 主机
port: 端口
hostname: 主机名
hash: 片段部分,也就是URL#之后的部分
search: url中HTTP GET的信息,包含了 ?
query: 和 search一样,但是不包含 ?
pathname: 跟在host之后的整个文件路径。但是不包含 ? 及 ?之后的字符串。
path: 和pathname一样,但是包含 ? 及之后的字符串,但是不包含hash
href: 原始的url
1.2 format方法。
format方法与parse方法相反,用于根据某个对象生成URL的字符串。
比如如下代码:
const url = require('url');var urlObj = {protocol: 'http:',slashes: true,auth: null,host: 'www.nodejs.org',port: null,hostname: 'www.nodejs.org',hash: '#about',search: '?with=query¶m=that',query: 'with=query¶m=that',pathname: '/some/url/',path: '/some/url/?with=query¶m=that',href: 'http://www.nodejs.org/some/url/?with=query¶m=that#about' }; console.log(url.format(urlObj));
最后输出:http://www.nodejs.org/some/url/?with=query¶m=that#about
1.3 resolve方法
resolve(from, to) 方法用于拼接URL, 它根据相对URL拼接成新的URL;
如下代码:
const url = require('url');var str1 = url.resolve('/one/two/three', 'four');console.log(str1); // 输出 /one/two/four const str2 = url.resolve('http://www.baidu.com', 'four'); console.log(str2); // 输出 http://www.baidu.com/four const str3 = url.resolve('http://www.baidu.com/', '/four'); console.log(str3); // 输出 http://www.baidu.com/four const str4 = url.resolve('http://www.baidu.com/one/', '/four'); console.log(str4); // 输出 http://www.baidu.com/four
二: querystring模块
它用于解析与格式化url查询字符串。
它提供了四个方法,分别是:querystring.parse, querystring.stringify, querystring.escape和querystring.unescape;
2.1 querystring.parse(string, separator, eq, options);
该方法是将一个字符串反序列化为一个对象。
string: 指需要反序列化的字符串。
separator(可选): 指用于分割字符串string的字符,默认为 &;
eq(可选): 指用于划分键和值的字符和字符串,默认值为 "=";
options(可选): 该参数是一个对象,里面可设置 maxKeys 和 decodeURIComponent 这两个属性。
maxKeys: 传入一个nmuber类型,指定解析键值对的最大值,默认值为1000, 如果设置为0,则取消解析的数量限制。
decodeURIComponent: 传入一个function, 用于对含有特殊字符进行编码。
如下代码:
const url = require('url'); const querystring = require('querystring'); const urlString = url.parse('http://www.nodejs.org/some/url/?with=query¶m=that#about');console.log(urlString); /*{protocol: 'http:',slashes: true,auth: null,host: 'www.nodejs.org',port: null,hostname: 'www.nodejs.org',hash: '#about',search: '?with=query¶m=that',query: 'with=query¶m=that',pathname: '/some/url/',path: '/some/url/?with=query¶m=that',href: 'http://www.nodejs.org/some/url/?with=query¶m=that#about' }} */ const str = querystring.parse(urlString.query); console.log(str); /*返回 { with: 'query', param: 'that' } */
// 如果是以井号隔开的话,那么使用后面的参数
const char = "with=query#param=that";
// 输出 { with: 'query', param: 'that' }
const str2 = querystring.parse(char, '#', null, {maxKeys: 2});
2.2 querystring.stringify(obj, separator, eq, options);
该方法是将一个对象序列化成一个字符串。
参数:obj指需要序列化的对象;
separator(可选),用于连接键值对的字符或字符串,默认为 &;
eq(可选),用于连接键和值的字符或字符串,默认值为 "=";
options(可选),传入一个对象,该对象设置 encodeURIComponent这个属性;
encodeURIComponent:值的类型为function,可以将一个不安全的url字符串转换成百分比的形式,默认值为querystring.escape()。
如下代码:
const url = require('url'); const querystring = require('querystring');// 如果是以井号隔开的话,那么使用后面的参数 const char = "with=query#param=that";const str2 = querystring.parse(char, '#', null, {maxKeys: 2}); // 输出 { with: 'query', param: 'that' } const str3 = querystring.stringify(str2); console.log(str3); // 输出 with=query¶m=that// 如下使用 * 号分割,使用$符号链接 const str4 = querystring.stringify({name: 'kongzhi', sex: [ 'man', 'women' ] }, "*", "$"); console.log(str4); // name$kongzhi*sex$man*sex$women
2.3 querystring.escape(str);
escape该方法可使传入的字符串进行编码, 如下代码:
const url = require('url'); const querystring = require('querystring');// 如果是以井号隔开的话,那么使用后面的参数 const char = "name=空智&sex=男";const res = querystring.escape(char); console.log(res); // 输出 name%3D%E7%A9%BA%E6%99%BA%26sex%3D%E7%94%B7
2.4 querystring.unescape(str);
该方法是对 使用了 escape编码的字符进行解码;如下代码:
const url = require('url'); const querystring = require('querystring');// 如果是以井号隔开的话,那么使用后面的参数 const char = "name=空智&sex=男";const res = querystring.escape(char); console.log(res); // 输出 name%3D%E7%A9%BA%E6%99%BA%26sex%3D%E7%94%B7 const res2 = querystring.unescape(res); console.log(res2); // name=空智&sex=男
转载于:https://www.cnblogs.com/tugenhua0707/p/9064292.html
nodeJS---URL相关模块用法(url和querystring)相关推荐
- NodeJs入门常见模块
NodeJs 常见模块 内置模块 fs 引入模块 const fs=require('fs');内置模块 常用方法: readFile 读文件 格式:fs.readfile(url,"utf ...
- Vue-引入querystring模块获取url参数
Vue-引入querystring模块获取url参数 querystring.parse(str,separator,eq,options) querystring.stringify(obj,sep ...
- Node中的Http模块和Url模块的使用
场景 如果我们编写后端的代码时,需要Apache 或者Nginx 的HTTP 服务器, 来处理客户端的请求相应.不过对Node.js 来说,概念完全不一样了.使用Node.js 时, 我们不仅仅在实现 ...
- JS获取请求URL相关参数
今天在找获取当前网址除去参数的js方式,结果自己会的竟然只有window.location.href 先看一个示例 用javascript获取url网址信息 <script type=" ...
- AS:Flash AS3中获取浏览器信息及URL相关参数(并非swf url地址)
原文链接:AS:Flash AS3中获取浏览器信息及URL相关参数(并非swf url地址) 好久没来这里了,最近发现网络上对此类信息的封装少的可怜,没有一个是比较完整的,今天又是周未,不敲点代码手痒 ...
- nodejs基础 ps模块常用API用法
nodejs的fs模块就是针对文件和文件夹进行一系列的操作 常用的fs模块API(感兴趣的可以去node官网看更多的) fs.readFile():用来读取文件内容的函数 fs.readdir():读 ...
- nodejs中的路径和url操作
const path = require('path') //路径模块 const url = require('url') //url模块 console.log(__dirname) // __d ...
- 17.3.12---urlparse模块的URL下载
1---urlparse模块是一个解析与泛解析Web网址URL字符串的一个工具 urlparse模块会将一个普通的url解析为6个部分,返回的数据类型都是元祖,同时,他还可以将已经分解后的url在组合 ...
- 爬虫实战学习笔记_3 网络请求urllib模块:设置IP代理+处理请求异常+解析URL+解码+编码+组合URL+URL连接
1 设置IP代理 1.1 方法论述 使用urllib模块设置代理IP是比较简单的,首先需要创建ProxyHandler对象,其参数为字典类型的代理IP,键名为协议类型(如HTTP或者HTTPS),值为 ...
- url散列算法原理_如何列出与网站相关的所有URL
url散列算法原理 by Ty Irvine 由Ty Irvine 如何列出与网站相关的所有URL (How to List Out All URLs Associated With a Websit ...
最新文章
- 耶鲁大学等机构提出的脑机接口软硬件协同设计,增加脑机的更大潜力
- 呼和浩特机器人光缆设备_工业机器人最坚固配件,虐它千万次,性能依旧
- SQL之inner join/left join/right join
- 使用 Azure Container Registry 储存镜像
- c 语言 小波变换,小波变换C语言
- python创建树_python – 从SQLalchemy中的自引用表创建树
- 研究所月入两万?见过越上班工资越少的骚操作吗...
- SVN如何回滚到指定版本
- 论文阅读220403_Autonomous Driving on Curvy Roads Without Reliance on Frenet Frame: A Cartesian-Based
- html调取android手机录音并保存,html5网页录音插件Recorder
- 不用任何软件!PDF转Word用微信这个功能,简单又方便!
- Pr 音频效果参考:混响
- 中标麒麟服务器性能怎么样,中标麒麟Linux系统的性能分析及工具(74页)-原创力文档...
- 推广的euclid_欧几里得(Euclid)与拓展的欧几里得算法
- C++中的重载丶重写丶重定义丶重定向的区别
- 2022-2027年中国汽油市场规模现状及投资规划建议报告
- 部落卫队 2281
- 快速排序的枢轴(pivot)和边界
- PDF文件的身份证号码
- sql注入原理及解决办法