admin管理员组

文章数量:1357273

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?

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?

Share Improve this question asked Jun 29, 2016 at 19:34 perennial_perennial_ 1,8462 gold badges28 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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

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

本文标签: nodejsDetermine if path is valid javascriptStack Overflow