简体   繁体   中英

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?

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");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM