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 badges1 Answer
Reset to default 8The 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");
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744075506a4554325.html
评论列表(0条)