Nodejs Chapter 12 (path)

The path module is different in different operating systems (windows | posix)

windowsYou must be familiar with it, posixyou may not have heard of it

posix(Portable Operating System Interface of UNIX)

posix stands for Portable Operating System Interface, which defines a set of standards. The operating systems that comply with this set of standards are (unix, like unix, linux, macOs, windows wsl). Why should this set of standards be defined, such as starting in a Linux system? A process needs to call forka function, and starting a process in Windows needs to call creatprocessa function, so there will be problems. For example, I have written code in Linux, and I need to port it to Windows and find that the functions are not uniform. The emergence of the POSIX standard is to solve this problem.

Windows does not fully follow the POSIX standard. Windows is designed to represent paths differently than POSIX.

On Windows systems, paths use backslashes ( \) as path separators. /This is different from the forward slash ( ) used by POSIX systems . This is due to the history of the Windows system, where earlier Windows operating systems made different design choices.

windows posix differences

path.basename()The method returns the last part of the given path

Handle windows paths in posix

path.basename('C:\temp\myfile.html');
// 返回: 'C:\temp\myfile.html'

The result returned does not correspond to myfile.html

If you want to process the windows path in the posix system and need to call the method of the corresponding operating system, you should modify it to

path.win32.basename('C:\temp\myfile.html');

return to myfile.html

path.dirname

This API is exactly complementary to basename

path.dirname('/aaaa/bbbb/cccc/index.html')

The dirname API returns /aaaa/bbbb/cccc except the last path.

basename API returns the last path index.html

path.extname

This API is used to return the extension. For example, /bbb/ccc/file.txt returns .txt

path.extname('/aaaa/bbbb/cccc/index.html.ccc.ddd.aaa')
//.aaa

If there are multiple . Return the last one. If there is no extension, return empty

path.join

This API is mainly used to splice paths

path.join('/foo','/cxk','/ikun')
// /foo/cxk/ikun

can support the ... ./ .../ operator

path.join('/foo','/cxk','/ikun','../')
// /foo/cxk/

path.resolve

Used to resolve relative paths and return绝对路径

If multiple absolute paths are passed in it will return the rightmost absolute path

path.resolve('/aaa','/bbb','/ccc')
//   /ccc

Pass in absolute path + relative path

path.resolve(__dirname,'./index.js')
//  /User/xiaoman/DeskTop/node/index.js

If only relative paths are passed in

path.resolve('./index.js')
// 返回工作目录 + index.js

path.parse path.format

path.format and path.parse are exactly complementary

parse

Used to resolve file paths. It takes a path string as input and returns an object containing the various components of the path

path.parse('/home/user/dir/file.txt')

{
    
    
  root: '/',
  dir: '/home/user/dir',
  base: 'file.txt',
  ext: '.txt',
  name: 'file'
}
  • root: The root directory of the path, ie  /.
  • dir: The directory where the file resides, ie  /home/user/documents.
  • base: filename, ie  file.txt.
  • ext: file extension, ie  .txt.
  • name: filename stripped of extension, ie  file.

format does the opposite in converting the object back to a string

path.format({
    
    
    root: '/',
    dir: '/home/user/documents',
    base: 'file.txt',
    ext: '.txt',
    name: 'file'
 })
 // /home/user/dir/file.txt

Guess you like

Origin blog.csdn.net/qq1195566313/article/details/132338169