node的url模块,和queryString和path模块的常用API的简介一下

path:

path模块是专门用来解析node的文件夹和的文件的路径

path的模块的使用方法:

/*
* node的原生模块:path
* path模块提供了一些实用工具,用于处理文件和目录的路径
*
* */
const path = require('path');

node的常用API:

1.path.join();是拼接路径的,一般配合node的全局变量__dirname使用得到一个安全的路径

console.log(path.join('./a', '/b')); //a\b
//返回1文件的路径,通过拼接写出绝对路径,就是找到当前文件的绝对路径,然后以当前模块的路径
// 根据相对路径去找
console.log(path.join(__dirname + '/1.txt'));//D:\candyVue\Node\path\1.txt

2.path.parse()把路径序列化,也就是细化,分成不同的模块

const path = require('path');
//parse 序列化,把我们的输入的路径序列化
console.log(path.parse('C:user/a/e/a.txt'));
/*
* {
  root: 'C:',  //根目录
  dir: 'C:user/a/e',  //文件夹
  base: 'a.txt',  //文件
  ext: '.txt', //后缀
  name: 'a'  //文件名
}

*
*
* */

3.path.relative();//输入两个参数路径,返回从第一个路径到第二个路径应该怎么走

//path.relative()  //返回是路径a到路径b要怎
// 么走,返回的是相对路径
console.log(path.relative('a/b', 'a/b/c'));  //c

4.resolve直接返回当前文件所在目录的绝对路径,

//path.resolve();会直接解析成绝对路径,拼接起来
const path = require('path');
console.log(path.resolve('a'));  //返回绝对目录D:\candyVue\Node\path\a

URL模块

url模块是专门用来解析和处理url路径的

用法:

const url = require('url');  //返回的输一个对象
console.log(url);  //url.URL是一个构造函数,要用new的方式显示出来
let u  = new url.URL('http://nodejs.cn/api/url.html?name=liquan&password=1234');
console.log(u);
//u的对象如下
/*
*   href: 'http://nodejs.cn/api/url.html',
  origin: 'http://nodejs.cn',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'nodejs.cn',
  hostname: 'nodejs.cn',
  port: '',
  pathname: '/api/url.html',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

注意:searchParams是map数据格式,是es6新增的和数组一样,是一种数据格式,里面的是方法,如果想要查找url的数据格式直接调用url对象下的属性就可以了

queryString模块:

queryString模块书专门用来解析和格式化URL路径的实用工具

使用:

const query = require('querystring');
// console.log(queryString);,专门用来 解析 和 格式化 URL字符串 的实用工具
//1.query.decode();是。parse的别名
//2.query.encode()是query.stringify()的别名
console.log(query.parse('http://nodejs.cn/api/querystring.html')); //处理为对象的数据格式,解开查询的url字符串
//.stringify();//把一个对象转换为一个url字符串
console.log(query.stringify({name: 'liquan', age: 18}));
//注意query.stringify(),有三个参数,第二个参数是连接的符号默认是&
//第三个参数是键值对的分割默认是=
console.log(query.stringify({name: 'liquan', age: 12}, ';', ':'));;

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值