node 内置模块

node内置模块

url

url模块常用于请求地址解析
usr.parse(url,bool)
仅当二参为true,则将url中的参数解析成对象放在query属性中

const url = require('url');
const strUrl1 = 'http://www.lagou.com/a/b?name=zhangsan&pswd=123456';
var rs1 = url.parse(strUrl1);
var rs2 = url.parse(strUrl1,true);//true把get请求参数解析成对象
console.log(rs1);
console.log(rs2);

打印结果

url.format(paramobj)
根据参数对象生成url

const url = require('url');
var params = {
    protocol:'https:',
    host:'www.lagou.com',
    hash:'#position',
    search:'?name=zhangsan%age=27',
    pathname:'/a/b'
}
//根据参数对象生成路径
var rs = url.format(params);
console.log(rs);//https://www.lagou.com/a/b?name=zhangsan%age=27#position

url.resolve(url,replaceStr)
替换url第一个/后的内容

const url = require('url');
const strUrl = 'http://www.lagou.com/a/b?name=zhangsan&pswd=123456';
console.log(url.resolve(strUrl,'!!!replace!!!'));//http://www.lagou.com/a/!!!replace!!!

path

  • path.resolve(__dirname,replaceStr)__dirname是node内置对象,代表当前环境路径.方法用于将replacestr添加到指定路径的后面
const path = require('path');
var rs = path.resolve(__dirname,"!!!replace!!!")
console.log(rs);//D:\part3\day0316\server\!!!replace!!!

querystring

querystring.parse(url)
用于将url请求参数转换为对象

const qs = require('querystring');
const strUrl = 'http://www.lagou.com/a/b?name=zhangsan&pswd=123456';
var rs = qs.parse(strUrl);
console.log(rs); //{ 'http://www.lagou.com/a/b?name': 'zhangsan', pswd: '123456' }

猜你喜欢

转载自www.cnblogs.com/ltfxy/p/12508091.html