Analysis of the advantages of path.join() in Node.js

Analysis of the advantages of path.join() in Node.js

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() method

The 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() method

The 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)

Recommend

vue uses Ele.me UI to imitate the filtering function of teambition

Table of contents Problem Description The general...

Nginx access log and error log parameter description

illustrate: There are two main types of nginx log...

A brief discussion on how to elegantly delete large tables in MySQL

Table of contents 1. Truncate operation 1.1 What ...

Solve the problem of managing containers with Docker Compose

In Docker's design, a container runs only one...

How to set the text in the select drop-down menu to scroll left and right

I want to use the marquee tag to set the font scro...

Detailed explanation of common Docker commands

1. Help Command 1. View the current Docker versio...

Steps to split and compress CSS with webpack and import it with link

Let's take a look at the code file structure ...

Mysql keeps the existing content and adds content later

This command modifies the data table ff_vod and a...

Detailed example of IOS database upgrade data migration

Detailed example of IOS database upgrade data mig...

Example code for implementing a text marquee with CSS3

Background Here's what happened, Luzhu accide...