使用require vs fs.readFile读取json文件内容

使用require vs fs.readFile读取json文件内容,第1张

使用require vs fs.readFile读取json文件内容

我想您将JSON.parse JSON文件进行比较,在这种情况下

require
会更好,因为它将立即解析该文件并进行同步

var obj = require('./myjson'); // no need to add the .json extension

如果您使用该文件有成千上万的请求,则在请求处理程序之外一次请求它,就是这样:

var myObj = require('./myjson');request(options, function(error, response, body) {   // myObj is accessible here and is a nice Javascript object   var value = myObj.somevalue;   // compare response identifier value with json file in node   // if identifier value exist in the json file   // return the corresponding value in json file instead});


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5001786.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存