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)
I like to pay attention to some news on weekdays a...
Because we were going to build a website, in addi...
Table of contents Problem Description The general...
illustrate: There are two main types of nginx log...
Table of contents 1. Truncate operation 1.1 What ...
In Docker's design, a container runs only one...
I want to use the marquee tag to set the font scro...
1. Help Command 1. View the current Docker versio...
Let's take a look at the code file structure ...
This command modifies the data table ff_vod and a...
1. MacVlan There are many solutions to achieve cr...
Detailed example of IOS database upgrade data mig...
Table of contents 1. Character Function 1. Case c...
When multiple images are introduced into a page, ...
Background Here's what happened, Luzhu accide...