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') // ../..
    
  • 相关阅读:
    RabbitMQ从入门到精通(一)
    MQ的架构作用
    Docker可视化管理工具
    Linux修改war包中文件
    Redis--各个数据类型最大存储量
    linux中直接修改jar包内配置文件
    脚本发布程序
    maven 安装到私服
    HTML基础 text-indent 把文字移出浏览器,隐藏起来
    HTML基础 td valign 设置文本靠上 居中 靠下
  • 原文地址:https://www.cnblogs.com/bradleyxin/p/14508514.html
Copyright © 2011-2022 走看看