Get the App
SLTechnology News&Howtos  ›  Development  › 

How does NodeJS detect whether the target is a file or a directory?

Shulou Source: shulou.com Published: 2022-06-03 17:12:04 09月22日 Update

This article mainly explains "how to detect whether the target is a file or a directory by NodeJS". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "NodeJS how to detect whether the target is a file or a directory"!

1. Check whether the file in the path is a directory or a file through fs.stat.

/ * learnNode.js * / let fs = require ('fs'); fs.stat ('. / js/newJs.js', (error, stats) = > {if (error) {console.log (error); return false;} else {console.log (`whether a file: ${stats.isFile ()}`); console.log (`whether a directory: ${stats.isDirectory ()}`); return false;}})

2. Star () mainly receives two parameters, the first is the file to be detected, and the second is a callback function, which has two parameters.

3. Err error and stats object, respectively.

This object provides information about the file, which we can judge.

At this point, I believe that everyone on the "NodeJS how to detect the target is a file or directory" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File directory detection or target object two information content function parameter learning practical deeper interest practical simple operation method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno Redmi MySQL vpn