Verifying Path Existence

Before doing any kind of read/write operation on a file or directory, you might want to verify whether the path exists. You can easily do this by using one of the following methods:

fs.exists(path, callback)
fs.existsSync(path)

fs.existsSync(path) returns true or false, depending on whether the path exists. Just as with any other asynchronous file system call, if you use fs.exists(), you will need to implement a callback that will be executed when the call completes. The callback will be passed a Boolean value of true or false, depending on whether the path exists. For example, the following code verifies the existence of the file named filesystem.js in the current path and displays the results:

fs.exists('filesystem.js', function (exists) {
  console.log(exists ? "Path Exists" : "Path Does Not Exist");
});

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.103.74