Nodejs教程12:path(路径)模块

栏目: Node.js · 发布时间: 6年前

内容简介:接下来通过一些例子熟悉一下path的使用:代码示例:/lesson12/path.js值得一提的是path.resolve方法,它可以接收任意个参数,然后根据每个路径参数之间的关系,将路径最终解析为一个绝对路径。

接下来通过一些例子熟悉一下path的使用:

代码示例:/lesson12/path.js

const path = require('path')

const str = '/root/a/b/1.txt'

console.log(path.dirname(str))  // 获取文件目录:/root/a/b
console.log(path.basename(str)) // 获取文件名:1.txt
console.log(path.extname(str)) // 获取文件后缀:.txt
console.log(path.resolve(str, '../c', 'build', 'strict')) // 将路径解析为绝对路径:C:\root\a\b\c\build\strict
console.log(path.resolve(str, '../c', 'build', 'strict', '../..', 'assets')) // 将路径解析为绝对路径:C:\root\a\b\c\assets
console.log(path.resolve(__dirname, 'build')) // 将路径解析为绝对路径:C:\projects\nodejs-tutorial\lesson12\build
复制代码

值得一提的是path.resolve方法,它可以接收任意个参数,然后根据每个路径参数之间的关系,将路径最终解析为一个绝对路径。

__dirname指的是当前模块所在的绝对路径名称,它的值会自动根据当前的绝对路径变化,等同于path.dirname(__filename)的结果。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web 2.0 Architectures

Web 2.0 Architectures

Duane Nickull、Dion Hinchcliffe、James Governor / O'Reilly / 2009 / USD 34.99

The "Web 2.0" phenomena has become more pervasive than ever before. It is impacting the very fabric of our society and presents opportunities for those with knowledge. The individuals who understand t......一起来看看 《Web 2.0 Architectures》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具