search
HomeWeb Front-endJS TutorialHow is the path object of nodeJs used to handle directories? (code)

这篇文章给大家介绍的内容是关于nodeJs的path对象是如何用来处理目录的?(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1 、path.dirname(arg)

const path=require('path');

path.dirname('dist/js/a.js');
//返回  dist/js,当dirname的参数agr为文件时候,返回改文件的目录

path.dirname(path.resolve('a','b','d/e'));
//返回D:\demos\a\b\d 绝对路径参数时候,返回上层目录

path.dirname('a/b/c')
//返回 a/b 参数为相对路径时,返回上层目录

//path.dirname用于路径,其中参数为字符串,其为文件时,返回文件所在目录;其为目录时,返回上层目录

2 、 path.resolve(arg1,arg2,….)

const path=require('path');

path.resolve('a','b','c/d');
//返回D:\a\b\c\d ,path.resolve返回当前环境所在路径拼接参数字符串所得到的绝对路径,其中参数可以有多个

3 、 path.relative(argS,argE)

const path=require('path');

path.relative('a/b/c','a/d/e');
//返回 ..\..\d\e ,

path.relative('D:/A/B/C','B/D/E');
//返回 ..\D\E , 

path.relative('C:/A/B/C','D:/A/D/E');
//返回 D:\A\D\E ,

//path.relative(argS,argE)方法用于获取从argS进入argE的相对路径,当两个参数都为绝对路径,且不同盘时,返回参数areE

4 、 path.join(arg1,arg2,arg3…)

const path=require('path');

path.join('a','b');
//返回 a\b

path.join(__dirname,'c','main.js')
//返回 D:\c\main.js  ,__dirname表示当前运行环境绝对路径

//path.join()方法拼接路径,并返回该路径,结合__dirname可以达到path.resolve()方法同样的效果

5 、 path.normalize(arg)

path.normalize('./a/./b')
//返回 a\b ---规则1:路径转换会把./去掉

path.normalize('a/b/../c/d/..')
//返回 a\c ---规则2:转换路径遇到 ../ ,则会去除本身与父目录

path.normalize('a////b/d///')
//返回 a\b\d ---规则3:多个连着斜杠会被解析为单斜杠,若单斜杠结尾,则保留

//path.normalize()方法接收一个参数,即为被解析的路径字符串,将其转换为标准路径,标准路径转换规则如上所示。

6 、 paht.basename(arg1,arg2)

const path=require('path');

path.basename('a/b/a.min.js');
//返回a.min.js,当参数arg2为空时,返回参数arg1中全文件名


path.basename('a/b/main');
//返回main,当参数arg2为空时,且arg1位路径时,返回参数arg1中最底层路径

path.basename('a.js.map','map')
//返回a.js ,匹配参数arg2的扩展名,去除并返回文件名

path.basename('a.js.map','.js')
//返回a.js.map ,匹配参数arg2的扩展名,如果匹配不到该扩展名,则直接返回全文件名

//path.basename()方法就是返回路径中文件名字而已,arg2增加了一步匹配并去除扩展名

7 、 path.extname(arg)

path.extname('a.js')
//返回扩展名.js(注意,有点.)

path.extname('a.js.map')
//返回扩展名.map

path.extname()
//报错

path.extname('a')
//报错

//path.extname()方法,有一个必须参数,其必须带有扩展名,否则报错,正常,则返回所传参数扩展名

8 、 path模块其他属性/方法

path.sep //返回操作系统中文件分隔符; window是'\\', Unix是'/'

path.delimiter //返回操作系统中目录分隔符,如window是';', Unix中是':'

path.isAbsolute(path) //检测path是否为绝对路径。一个绝对路径会解析到相同的位置,无论是不是在工作目录。

path.parse(pathString) //返回路径字符串的对象。

path.format(pathObject) //从对象中返回路径字符串,和 path.parse 相反。

相关推荐:

nodejs读取Excel数据以及下载图片的代码实现

The above is the detailed content of How is the path object of nodeJs used to handle directories? (code). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function