2020年2月13日 星期四

[NodeJS 文章收集] NodeJS accessing file with relative path

Source From Here
Question
It seemed like a straight forward problem. But I am not able to crack this. Within helper1.js I would like to access foobar.json (from config/dev/)
  1. root  
  2.   -config  
  3.    --dev  
  4.     ---foobar.json  
  5.   -helpers  
  6.    --helper1.js  
Any help here would be great.

How-To
You can use the path module to join the path of the directory in which helper1.js lives to the relative path of foobar.json. This will give you the absolute path to foobar.json:
  1. var fs = require('fs');  
  2. var path = require('path');  
  3.   
  4. var jsonPath = path.join(__dirname, '..''config''dev''foobar.json');  
  5. var jsonString = fs.readFileSync(jsonPath, 'utf8');  

1 則留言:

  1. Strange "water hack" burns 2 lbs overnight

    Over 160 thousand women and men are losing weight with a simple and secret "liquids hack" to lose 2 lbs each night in their sleep.

    It's easy and works on everybody.

    This is how you can do it yourself:

    1) Take a drinking glass and fill it with water half full

    2) And then learn this weight loss HACK

    and become 2 lbs lighter the next day!

    回覆刪除

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...