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

Tutorial on installing mongodb under linux

MongoDB is cross-platform and can be installed on...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

Solution to the problem of not finding Tomcat configuration in Intelli Idea

I joined a new company these two days. The compan...

Implementation of local migration of docker images

I've been learning Docker recently, and I oft...

Vue+echart realizes double column chart

This article shares the specific code of vue+echa...

How to generate Hive table creation statement comment script in MySQL metadata

Preface This article mainly introduces the releva...

JavaScript to achieve a simple page countdown

This article example shares the specific code of ...

MySQL optimization solution: enable slow query log

Table of contents Preface Setting up slow query l...

Summary of using the exclamation mark command (!) in Linux

Preface Recently, our company has configured mbp,...

MySQL big data query optimization experience sharing (recommended)

Serious MySQL optimization! If the amount of MySQ...