perennial_
perennial_

Reputation: 1846

Determine if path is valid javascript

I have a path which is passed down to me as a javascript variable, which will reference to an image (maybe). ../app/assets/icon.png. In the case that the path is invalid or doesn't exist, I want to use a different file, located somewhere else. In the end, it should look something like this:

var verifiedPath = existsAndIsValid(path) ? path : '../app/default/icon.png'

Is there a simple one-liner in which I can do this?

Upvotes: 5

Views: 4740

Answers (1)

Quentin
Quentin

Reputation: 943142

The very simple exists function is deprecated and recommends using stat or access instead. They are all core NodeJS.

fs.access('path', fs.R_OK, (err) => {
  if (!err) { console.log("File exists");
});

Upvotes: 8

Related Questions