node中的url模块、path模块

9 篇文章 0 订阅

 注意:不要用let url=require("url");url.parse();已失效。要用new URL()

            let { origin,pathname } = new URL(item)

            // decodeURIComponent()函数将中文乱码转为中文

            arr_pathname.push(origin +decodeURIComponent(pathname))

url.parse() :将url字符串转为url对象

url.format() :将url对象转为url字符串

var url = require('url');
var obj1 = {
    protocol: 'http:',
    slashes: true,
    auth: null,
    host: '120.76.197.111:9897',
    port: '9897',
    hostname: '120.76.197.111',
    hash: null,
    search: '?username=admin&password=123',
    query: {
        username: 'admin',
        password: '123'
    },
    pathname: '/login/',
    path: '/login/?username=admin&password=123',
    href: 'http://120.76.197.111:9897/login/?username=admin&password=123'
};

console.log(url.format(obj1));

path.extname():获取文件后缀名

path.parse():将一个字符串类型的路径转化为一个路径对象(pathObject)。该路径对象当中包括文件目录,文件名,扩展名等。

path.resolve()

const path = require("path")
console.log("__dirname:", __dirname) //G:\projects\WeServerManage\server
console.log("__filename:", __filename)  //G:\projects\WeServerManage\server\app.js
console.log(path.resolve());  //G:\projects\WeServerManage\server
console.log(path.resolve(''));  //G:\projects\WeServerManage\server
console.log(path.resolve('.'));  //G:\projects\WeServerManage\server
console.log(path.resolve(__dirname));  //G:\projects\WeServerManage\server
console.log(path.resolve('js/common', 'test'));  //G:\projects\WeServerManage\server\js\common\test
console.log(path.resolve('./js/common', 'test')); //G:\projects\WeServerManage\server\js\common\test
console.log(path.resolve('js/common', './test')); //G:\projects\WeServerManage\server\js\common\test
console.log(path.resolve('./js/common', './test'));  //G:\projects\WeServerManage\server\js\common\test
console.log(path.resolve('/js/common', 'test'));  //G:\js\common\test
console.log(path.resolve('js/common', '/test'));  //G:\test
console.log(path.resolve('/js/common', '/test'));  //G:\test
console.log(path.resolve('/js/common', '../test'));  //G:\js\test
console.log(path.resolve('js/common', '../test'));  //G:\projects\WeServerManage\server\js\test

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值