Importing a JSON file in Meteor

前端 未结 3 899
清歌不尽
清歌不尽 2020-12-07 17:41

I have a data.json file that I would like to load and that I have placed in the lib/ folder. What should I do in order to load that JSON into a variable in the server? Thank

3条回答
  •  情话喂你
    2020-12-07 18:13

    I assume you want the json content to be represented as an object and not as a simple string.

    I use js-yaml (https://github.com/nodeca/js-yaml), assuming you install the npm package. You can also just copy it manually.

    yaml = __meteor_bootstrap__.require('js-yaml')
    fs = __meteor_bootstrap__.require('fs')
    content = fs.readFileSync(file, 'utf8')
    object = yaml.load(content)
    

    and that's it! I personally persist my json into meteor collections.

提交回复
热议问题