nodejs之path

node

node-path

使用之前首先引入 path
import path from 'path';

1.path.join()

  • 使用特定的分隔符作为定界符将所有给定的 path 片段连接在一起,然后规范化生成的路径。(拼接路径)
    path.join('http://localhost:4001/', '/news') // http:\localhost:4001\news
    • windows 中
      var str=path.join('/foo', 'bar', 'baz/asdf'); // \foo\bar\baz\asdf

2.path.dirname()

  • 返回文件中目录名
    path.dirname('http://localhost:4001/news/index.html') // http://localhost:4001/news
    • windows 中
      path.dirname('file:///D:/news/a.js'); // file:///D:/news

3.path.basename()

  • 返回 path 的最后一部分,(第二个参数为可选的文件拓展名)
    path.basename('http://localhost:4001/news/index.html') //index.html
    • 第二个参数
      path.basename('http://localhost:4001/news/index.html','.html') //index

4.path.extname()

  • 返回路径中的扩展名,即 path 中文件的后缀名,如果没有则返回空字符串
    path.extname('http://localhost:4001/news/index.html') //.html
    • windows 中
      path.extname('file:///D:/news/a.js') // .js

5.path.parse()

  • 将字符串转换为对象

    <!-- 返回值有以下属性 -->
    dir <string>
    root <string>
    base <string>
    name <string>
    ext <string>
    

    path.parse('http://localhost:4001/news/index.html') //{ root: '', dir: 'http://localhost:4001/news', base: 'index.html', ext: '.html', name: 'index' }

6.path.format()

  • 将 path 对象转换为字符串,与 path.parse()相反

    • windows 中
    var o = {
      root: '',
      dir: 'C:\\path\\dir',
      base: 'a.js',
      ext: '.js',
      name: 'a',
    };
    var str = path.format(o);
    //C:\path\dir\a.js
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值