zoukankan      html  css  js  c++  java
  • node path模块

    一、在nodejs中path模块时使用频率很高的模块,其中不乏有很多API写得很模糊,但仔细琢磨下来,也不是很难理解。

    1.获取文件所在路径

    var path = require('path');
    var test = '/test/test1/test2/test.js';
    //获取文件所在的目录
    console.log(path.dirname(test)); //  /test/test1/test2

    2.获取路径中的最后一部分

    var path = require('path');
    var test = '/test/test1/test2/test.js';
    var test1 = '/test/test1/test2/';
    var test2 = '/test/test1/test2';
    
    //获取路径中的最后一部分 大部分时间可以用来判断文件名
    console.log(path.basename(test)); //test.js
    console.log(path.basename(test1)); // test2
    console.log(path.basename(test2));//test2

    3.获取文件的拓展名

    var path = require('path');
    var test = '/test/test1/test2/test.js';
    var file1 = '.js';
    var file2 = 'js.';
    
    //获取文件的拓展名  其实是从最后一个 '.'开始截取的。特别需要注意的是
    //如果 '.'开头 则返回 空 , 点结尾返回 '.'
    console.log(path.extname(test)); //.js
    console.log(path.extname(file1)); // ''
    console.log(path.extname(file2)); //'.'

    二、组合路径

    1. 路径的拼接 path.join();

    var path = require('path');
    var test = '/test/';
    var test1 = '/test1/';
    var test2 = '/test2/';
    
    console.log(path.join(test,test1,test2));
    // '	est	est1	est2';
    // 为什么会输出这个呢 ? 其实是把 path 拼接起来在normalize一下

    2.path.resove(from,to);将to 解析为绝对路径

    var path = require('path');
    
    console.log(path.resolve('path.js')) //expresspath.js

    3.path.parse();路径的解析

    var path = require('path');
    
    //路径的解析
    console.log(path.parse('path.js'))
    //{ root: '', dir: '', base: 'path.js', ext: '.js', name: 'path' }

    三、规范路径 path.normalize();

    • 如果路径为空,返回.,相当于当前的工作路径。
    • 将对路径中重复的路径分隔符(比如linux下的/)合并为一个。
    • 对路径中的...进行处理。(类似于shell里的cd ..
    • 如果路径最后有/,那么保留该/
      var path = require('path');
      var filepath = '/tmp/demo/js/test.js';
      
      var index = 0;
      
      var compare = function(desc, callback){
          console.log('[用例%d]:%s', ++index, desc);
          callback();
          console.log('
      ');
      };
      
      compare('路径为空', function(){
          // 输出 .
          console.log( path.normalize('') );
      });
      
      compare('路径结尾是否带/', function(){
          // 输出 /tmp/demo/js/upload
          console.log( path.normalize('/tmp/demo/js/upload') );
      
          // /tmp/demo/js/upload/
          console.log( path.normalize('/tmp/demo/js/upload/') );
      });
      
      compare('重复的/', function(){
          // 输出 /tmp/demo/js
          console.log( path.normalize('/tmp/demo//js') );
      });
      
      compare('路径带..', function(){
          // 输出 /tmp/demo/js
          console.log( path.normalize('/tmp/demo/js/upload/..') );
      });
      
      compare('相对路径', function(){
          // 输出 demo/js/upload/
          console.log( path.normalize('./demo/js/upload/') );
      
          // 输出 demo/js/upload/
          console.log( path.normalize('demo/js/upload/') );
      });
      
      compare('不常用边界', function(){
          // 输出 ..
          console.log( path.normalize('./..') );
      
          // 输出 ..
          console.log( path.normalize('..') );
      
          // 输出 ../
          console.log( path.normalize('../') );
      
          // 输出 /
          console.log( path.normalize('/../') );
      
          // 输出 /
          console.log( path.normalize('/..') );
      });

    四、其中不乏有些没有说清楚,按照API写一遍,加上自己的理解,大概就清楚了。

    祝大家端午节快乐。

  • 相关阅读:
    关于VSCode如何缩进两个空格
    基于vue来开发一个仿饿了么的外卖商城(二)
    在复杂的项目开发中使用结对编程
    第一个博客
    docker容器启动失败解决办法
    windows转mac-开发环境搭建(六):mac上搭建git环境
    windows转mac-开发环境搭建(五):mac上用docker安装并运行mysql
    windows转mac-开发环境搭建(四):mac上搭建node、VS code、idea环境
    windows转mac-开发环境搭建(三):mac上搭建maven环境
    windows转mac-开发环境搭建(二):mac上搭建java环境
  • 原文地址:https://www.cnblogs.com/createGod/p/6919869.html
Copyright © 2011-2022 走看看