在我的流星应用程序中使用 nodejs fs 模块 [英] Using nodejs fs module within my meteor app
本文介绍了在我的流星应用程序中使用 nodejs fs 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要读取一些 json 文件,然后将数据加载到数据库中.我想使用 node.js 中的 fs 模块,而不是使用 HTTP.问题是我如何在流星应用程序中使用以下代码.
I need to read some json file and then load the data into the db. Instead of using HTTP I want to use fs module from node.js. The question is that how can I use the following code from within meteor app.
var fs = require('fs');
var file = __dirname + '/test.json';
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
console.dir(data);
});
推荐答案
如果你在一个包中,那么你需要做的就是将 require
替换为 NPM.require代码>:
If you are within a package, then all you need to do is to replace the require
with NPM.require
:
var fs = Npm.require('fs');
如果你想在一个项目中使用它(而不是在一个包中),那么只需将 meteorhacks:npm
添加到你的项目中,然后使用
If you want to use it in a project (not in a package), then just add the meteorhacks:npm
to your project and then use
var fs = Meteor.npmRequire('fs');
这篇关于在我的流星应用程序中使用 nodejs fs 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文