You might be wondering why you should use the path.join() method instead of string concatenation. '/path' + '/' + 'to' + '/' + 'test.txt' // '/path/to/test.txt' ['/path', 'to', 'test.txt'].join('/') // '/path/to/test.txt' 1. Support for Windows. Windows uses backslash (\) instead of forward slash (/) as path separators. path.join() takes care of this for us. Because path.join('data', 'test.txt') returns 'data/test.txt' on Linux and OSX as well as Windows. 2. Used to handle edge cases. When working with file system paths, many edge cases pop up. For example, if you try to concatenate two paths manually, you might accidentally end up with duplicate path separators. The path.join() method takes care of leading and trailing slashes for us. path.join('data', 'test.txt') // 'data/test.txt' path.join('data', '/test.txt') // 'data/test.txt' path.join('data/', 'test.txt') // 'data/test.txt' path.join('data/', '/test.txt') // 'data/test.txt' Knowledge point expansion: 1. path.join() methodThe path.join() method merges multiple parameter strings into a path string console.log(path.join(__dirname,'a','b')); If the path of the current file is E:/node/1, then the concatenated path is E:/node/1/a/b. console.log(path.join(__dirname,'/a','/b','…')); The / at the beginning of the path does not affect the concatenation, … represents the previous level file, and the concatenated result is: E:/node/1/a console.log(path.join(__dirname,'a',{},'b')); And path.join() will also help us to verify the path string. When the string is illegal, it will throw an error: Path must be a string. 2. path.resolve() methodThe path.resolve() method uses the program as the root directory as the starting point and resolves an absolute path based on the parameters With application as root directory A normal string represents a subdirectory / represents the absolute path root directory console.log(path.resolve()); Get the directory of the application startup file (get the absolute path of the current execution file) E:\zf\webpack\1\src console.log(path.resolve('a','/c')); E:/c , because the / slash represents the root directory, so the result is E:/c So we usually need to be careful when using / slash when splicing console.log(path.resolve(__dirname,'img/so')); E:\zf\webpack\1\src\img\so This is to concatenate the file paths, regardless of whether the path actually exists. console.log(path.resolve('wwwroot', 'static_files/png/', '…/gif/image.gif')) E:\zf\webpack\1\src\wwwroot\static_files\gif\image.gif This is the concatenation of the absolute path of the current application startup file and all the subsequent strings, because the initial string does not start with /. ...also represents the parent directory. This is the end of this article about the advantages of path.join() in Node.js. For more information about the advantages of path.join() in Node.js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: How to use MySQL covering index and table return
>>: Solution to Workbench not connecting to MySQL on Alibaba Cloud Server Ubuntu (tested)
MongoDB is cross-platform and can be installed on...
Back in the Kernel 2.6 era, a new security system...
Effect: css: .s_type { border: none; border-radiu...
I joined a new company these two days. The compan...
I've been learning Docker recently, and I oft...
Background: I want to install a SAP ECC server an...
This article shares the specific code of vue+echa...
Preface This article mainly introduces the releva...
<br />Green is between yellow and blue (cold...
Table of contents 1. Deploy consul cluster 1. Pre...
WebRTC, which stands for Web Real-Time Communicat...
This article example shares the specific code of ...
Table of contents Preface Setting up slow query l...
Preface Recently, our company has configured mbp,...
Serious MySQL optimization! If the amount of MySQ...