我是新手。在node.js
这里,我在node.js中编写了一个示例函数来json
按如下所示打印文件的内容。
exports.getData =function(callback) {
readJSONFile("Conf.json", function (err, json) {
if(err) { throw err; }
console.log(json);
});
console.log("Server running on the port 9090");
我在这里要做的只是读取json
文件并在控制台中打印内容。但是我不知道如何调用该getData
函数。运行此代码时,它仅打印sever running on the port..", not my
json`内容。
我知道上面的代码不正确
如何调用函数node.js
并打印json
内容?
Node.js只是普通的javascript。首先,似乎您缺少}
。由于它使问题更易于理解,因此我假设您console.log("Server...
不在exports.getData
。
您将像其他任何函数一样调用函数:
...
console.log("Server running on the port 9090");
exports.getData();
我会注意到您callback
在getData函数中有一个参数,但是您没有在调用它。也许应该这样称呼它:
exports.getData = function(callback) {
readJSONFile("Conf.json", function (err, json) {
if(err) { throw err; }
callback(json);
});
}
console.log("Server running on the port 9090");
exports.getData(function (json) {
console.log(json);
});
坦白说,您的getData函数有点多余,没有更多内容,因为它只做wrap readJSONFile
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句