jQuery 在 node.js 中调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19532808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call a function in node.js
提问by Sush
I am new to node.js
.Here I write a sample function in node.js to print the contents of a json
file as follows.
我是新手。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");
What I am doing here is I just want to read a json
file and print the contents in console. But I do not know how to call the getData
function. While running this code it only prints the sever running on the port..", not my
json` contents.
我在这里做的是我只想读取一个json
文件并在控制台中打印内容。但我不知道如何调用该getData
函数。运行此代码时,它只打印sever running on the port..", not my
json` 内容。
I know the above code is not correct
我知道上面的代码不正确
How can I call a function in node.js
and print the json
contents?
如何调用函数node.js
并打印json
内容?
回答by Los Frijoles
Node.js is just regular javascript. First off, it seems like you are missing a }
. Since it makes the question easier to understand, I will assume that your console.log("Server...
is outside exports.getData
.
Node.js 只是普通的 javascript。首先,您似乎缺少一个}
. 由于它使问题更容易理解,我将假设您console.log("Server...
在exports.getData
.
You would just call your function like any other:
您只需像其他函数一样调用您的函数:
...
console.log("Server running on the port 9090");
exports.getData();
I would note that you have a callback
argument in your getData function but you are not calling it. Perhaps it is meant to be called like so:
我会注意到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);
});
Truthfully, your getData function is a little redundant without any more content to it since it does nothing more than just wrap readJSONFile
.
说实话,你的 getData 函数有点多余,没有更多的内容,因为它只是 wrap readJSONFile
。
回答by Hubro
Don't take this the wrong way, but your code appears to be a mixed up mess of unrelated examples. I recommend you start by learning the basics of JavaScript and node.js (for example, read Eloquent JavaScriptand Felix's Node.js Beginners Guide).
不要误会,但您的代码似乎是一堆不相关的示例。我建议您首先学习 JavaScript 和 node.js 的基础知识(例如,阅读Eloquent JavaScript和Felix 的 Node.js 初学者指南)。
But on to your code. First of all, you are creating a function (called getData
) and exporting it. Then you're printing "Server running on the port 9090". There is no server code in your script, and the function you created is never executed.
但是到你的代码。首先,您正在创建一个函数(称为getData
)并将其导出。然后您正在打印“在端口 9090 上运行的服务器”。您的脚本中没有服务器代码,您创建的函数永远不会执行。
I think this is what you intended to write:
我想这就是你打算写的:
readJSONFile("Conf.json", function (err, json) {
if(err) { throw err; }
console.log(json);
});
Assuming that readJSONFile
is a real function.
假设这readJSONFile
是一个真正的函数。