Table of contents
1.
Introduction: 
2.
Node.js File stats:
3.
Node.js file stats:
4.
Getting information out of a path:
5.
Working with paths:
6.
Frequently asked questions:
7.
Key Takeaways:
Last Updated: Mar 27, 2024

Node.js file stats and paths

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction: 

In our previous blogs, we are familiar with what precisely Node.js is. In this article, we are concerned about how we can apply node.js file stats and node.js paths.

 Though their name is taken together, how they part their ways, let’s figure this out in detail.

                                                        Source: giphyMoney Apply GIF by ING Romania

Node.js File stats:

 

Every file has a bunch of details that can be reviewed by Node.js. Generally, the stat() method is used provided by the fs module.

 

It is called, passing a file path and once the file details are caught by the Node.js, it will call the callback function passed. It accepts two arguments: an error message and file stats.

 

const fs = require('fs')
fs.stat('/User/codingNinja/practise.txt', (eror, stats) => {
 if (eror) {
  console.error(eror)
  return
}
 // Accessing file stats in `stats`.
})

 

A sync method is also provided by node.js. This method blocks all threads until and unless the file stats are ready.

 

const fs = require('fs')
try {
 const stats = fs.statSync('/User/codingNinja/practise.txt')
catch (err) {
 console.error(err)
}

 

We know the file information is contained in the stats variable. Now, the question is what kind of information can be extracted using these stats.

 

The following things is contained in the stats:

  • If the file is a directory or a file, then prefer stats.isFile() and stats.isDirectory(). 
  • If the file is a symbolic link, then prefer stats.isSymbolicLink().
  • For the files in bytes, stats. size.

 

const fs = require('fs')
fs.stat('/User/codingNinja/practise.txt', (err, stats) => {
  if (err) {
    console.error(err)
    return
  }

  stats.isFile() // True.
  stats.isDirectory() // False.
  stats.isSymbolicLink() // False.
  stats.size // 1024000 = 1MB.
})

 

Now that we are comfortable with file stats in node.js. Let’s move forward towards the paths in node.js.

 

Node.js file stats:

A path is present for every file of a system. On Linux or macOS, the file path looks like this:

 

/user/codingNinja/practise.txt

 

But, the structure in windows look like this:

E:\user\codingNinja\practise.txt

 

The structure looks the same, but a minimal difference lies. Both the differences must be taken care of while using them. 

 

Getting information out of a path:

Let’s say you are provided a path. From it, the information can be extracted by the following:

  • dirname: gives parent holder of a file
  • basename: gives the file name
  • extname: given the file extension

 

const notes = '/user/codingNinja/practise.txt'

path.dirname(notes) // /user/codingNinja.
path.basename(notes) // practise.txt.
path.extname(notes) // .txt.

 

The filename can also be received without the usage of a second argument to the basename

 

path.basename(practise, path.extname(practise)) // Notes.

 

Working with paths:

Two or more paths can be joined using the path. join()

 

const name = 'codingNinja'
path.join('/''user', name, 'practise.txt'// '/user/codingNinja/practise.txt'.

 

The absolute path of a relative path can be calculated using the path. resolve().

 

path.resolve('codingNinja.txt'

//'/User/codingNinja/codingNinja.txt' if run from the home folder.

 

In the current working directory, node.js will only append /codingNinja.txt. If the second parameter folder is specified, resolve will be using the first as a base for the second. 

 

path.resolve('temp''codingNinja.txt'

// '/User/codingNinja/temp/codingNinja.txt'run from home folder.

 

The slash(“/”) here denotes that the path is an absolute one.

 

path.resolve('/etc''codingNinja.txt'// '/etc/codingNinja.txt'.

 

path. normalize() is one of the other useful functions. This function helps in calculating the actual path. This function comes into existence when it contains relative specifiers likeor .. or double slashes(//).

 

path.normalize('/user/codingNinja/..//practise.txt'//'/user/practise.txt'.

 

Both the methods normalize and never resolve checks for the existence of the paths. They are just concerned about finding or calculating the path depending on the information they get.

Also see, Difference Between Controller and Restcontroller

Frequently asked questions:

  1. Express error handling in Node.js?

 

Error handling is the way to express catches and process errors. It is done in both synchronous and asynchronous ways. Express has its error handler, so we don’t need to write on our own.

2. Await throws an error. Justify the statement.

 

If any promise fails to resolve typically, then await cannot return the result. In such cases, await throws an error.

3. Is node.js a programming language?

 

No, node.js is not a programming language. But, it makes use of javascript, which is a programming language. It also helps web developers to play and build web applications.

4. Name some uses of node.js?

 

The uses of node.js are:

  • Server-side programming
  • Deployed for non-blocking
  • Creating and deleting server files.

 

Key Takeaways:

In this article, we have learned about Node.js file stats and Node.js paths. We have also seen both of these with the help of different examples, how they should be used, what arguments they take, and what difference lies in their usage.

Keeping the theoretical knowledge at our fingertips helps us get about half the work done. To gain complete understanding, practice is a must. To gain thorough understanding, visit our page.

 

Happy coding

 

Live masterclass