zoukankan      html  css  js  c++  java
  • Node-Path模块

    Node.js 提供了 path 模块,用于处理文件路径和目录路径 . 不同操作系统 表现有所差异 !

    1. 获取路径的目录名

    const path = require('path')
    
    path.dirname('/path/example/index.js') // /path/example
    

    2. 获取路径的扩展名

    const path = require('path')
    
    path.extname('/path/example/index.js') // .js
    

    3. 是否是绝对路径

    const path = require('path')
    
    path.isAbsolute('/path/example/index.js') // true
    
    path.isAbsolute('.') // false
    

    4. 拼接路径片段

    path.join('/path', 'example', './index.js') // /path/example/index.js
    

    5. 将路径或路径片段的序列解析为绝对路径。

    path.resolve('/foo/bar', './baz')
    // 返回: '/foo/bar/baz'
    
    path.resolve('/foo/bar', '/tmp/file/')
    // 返回: '/tmp/file'
    
    path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
    // 如果当前工作目录是 /home/myself/node,
    // 则返回 '/home/myself/node/wwwroot/static_files/gif/image.gif'
    

    6. 规范化路径

    path.normalize('/path///example/index.js') //  /path/example/index.js
    

    7. 解析路径

    path.parse('/path/example/index.js')
    
    /*
     { root: '/',
      dir: '/path/example',
      base: 'index.js',
      ext: '.js',
      name: 'index' }
    */
    

    8. 序列化路径

    path.format({
      root: '/',
      dir: '/path/example',
      base: 'index.js',
      ext: '.js',
      name: 'index'
    }) // /path/example/index.js
    

    9. 获取 from 到 to 的相对路径

    path.relative('/path/example/index.js', '/path') // ../..
    
  • 相关阅读:
    Math.pow
    css3正方体
    制作一个百度换肤效果
    排他思想
    js栈和堆的区别
    js创建对象的几种方式(工厂模式、构造函数模式、原型模式)
    短网址
    this
    作用域
    JS 函数基础
  • 原文地址:https://www.cnblogs.com/bradleyxin/p/14508514.html
Copyright © 2011-2022 走看看