Home Web Front-end JS Tutorial How is the path object of nodeJs used to handle directories? (code)

How is the path object of nodeJs used to handle directories? (code)

Aug 02, 2018 am 10:33 AM
node.js

这篇文章给大家介绍的内容是关于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用于路径,其中参数为字符串,其为文件时,返回文件所在目录;其为目录时,返回上层目录
Copy after login

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

const path=require('path');

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

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
Copy after login

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()方法同样的效果
Copy after login

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()方法接收一个参数,即为被解析的路径字符串,将其转换为标准路径,标准路径转换规则如上所示。
Copy after login

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增加了一步匹配并去除扩展名
Copy after login

7 、 path.extname(arg)

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

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

path.extname()
//报错

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

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

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

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

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

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

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

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

相关推荐:

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

See all articles