How 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 相反。
相关推荐:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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!

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

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 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!

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.

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

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!

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!
