| by Arround The Web | No comments

How Does Node.js path.format() Method Work?

Node.js “path” module deals with the system files and directories path. It provides the simplest way to manage, alter, and access the paths as per requirements. Its common operations are, normalizing, finding directory/file names, extracting file extensions, formatting the paths into strings, and much more. All the particular operations can be performed by using its predefined methods and properties.

This post will explain the “path.format()” method in Node.js.

How Does Node.js path.format() Method Work?

The “format()” is the built-in method of the “path” module that converts the path object into the path string. The “path object” displays the path of the specified element as its properties in the key-value pair. Whereas the “path string” specifies the path of a particular file into the string format like “C:\\users\\Lenovo\\File\\Hello.html”.

Syntax

path.format( path object )

The above syntax supports only one parameter “path object” that specifies the path details using the following properties:

  • root: It specifies the root directory of the file.
  • dir: It tells the name of the directory in which the specified file is available.
  • base: It denotes the filename along with its extension.
  • ext: It represents only the file extension.
  • name: It displays only the file name.

Let’s apply the above-defined method practically.

Example 1: Applying “path.format()” Method on Windows

This example uses the “path.format()” method on Windows operating system:

const path = require('path');

var path_obj = {dir: "C:\\users\\Lenovo", base: "Hello.html"};

var result= path.format(path_obj)

console.log(result);

In the above code lines:

  • Firstly, the “require()” method includes the “path” module in the Node.js project.
  • Next, the “path-obj” variable specifies the particular file path as an object along with the following dir and the base properties.
  • After that, the “format()” method converts the specified path object into the string format.
  • Lastly, the “console.log()” method displays the output of the “format()” method stored in the “result” variable on the console.

Output

Run the below-stated command to execute the “.js” file:

node app.js

It can be seen that the terminal shows the file path in string format that is passed as an object:

Example 2: Applying “path.format()” Method on POSIX

This example applies “path.format()” method on POSIX(Portable Operating System Interface) based on UNIX operating system:

const path = require('path');

var path_obj = {dir: "/users/admin", base: "Hello.html"};

var result= path.format(path_obj)

console.log(result);

In the above code lines:

  • The “format()” method converts the path of the particular file into an object passed as its argument.
  • The “console.log()” method displays the output of the “format()” method.

Output

Execute the “.js” file:

node app.js

It can be observed that the output denotes the path of the specified file in string format:

That’s all about the working of the “path.format()” method in Node.js.

Conclusion

In Node.js, the “path.format()” method changes the path object into the string format. It takes the particular file path as the properties of the path object and then converts it into the string. It shortens the detailed path of a file into one line. This post has explained practically the “path.format()” method in Node.js.

Share Button

Source: linuxhint.com

Leave a Reply