node.js - Determine if path is valid javascript - Stack Overflow

I have a path which is passed down to me as a javascript variable, which will reference to an image (ma

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744075506a4554325.html

相关推荐

  • node.js - Determine if path is valid javascript - Stack Overflow

    I have a path which is passed down to me as a javascript variable, which will reference to an image (ma

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信