nodejscallback使用module.exports

我从NodeJS开始,并试图了解callback如何与module.exports相关。

我有两个NodeJS文件。

  1. s1.js接收input字段消息并将结果传回。 s1.js通过module.exports公开
  2. start.js包含s1.js(通过require),并接收我想在程序中使用的结果

start.js

var s1=require('./s1.js'); var r1; s1.test("Leon", function(err, result) { if (err){console.log("Error occurred "+err);} console.log("Callback after s1.test execution "+result); r1=result; } ); console.log("result "+r1); console.log("end"); 

s1.js

 module.exports.test=function test(msg,result){ result="Hello "+msg; console.log("Inside s1.js - "+result); }; 

当我执行start.js时,结果如下

 Inside s1.js - Hello Leon result undefined end 

正如所料,结果是未定义的,因为s1.test的callback尚未完成。
我不明白的是为什么从来没有达到s1.test的callback。

有任何想法吗?
谢谢莱昂

以前的答案是正确的,但是你的问题与nodejs或者module.exports没什么关系,但是问题是你还不了解JavaScript(以及PHP和其他语言)中使用的callback的本质。

你使用的例子实际上使这种差别更难理解。 所以这里是一个一步一步的描述如何理解callback,从简单的语句开始,看起来像你的代码结束。

 // Create some object var fooBar = { foo: 'foo object'}; // Add a bar function to that object fooBar.bar = function (param1, param2) { console.log("Inside bar function: " + param1); }; 

运行此代码不会执行任何操作,因为该对象已创build并添加了一个函数,但该函数从不会被调用。

为此,我们需要添加一个对该函数的调用:

 fooBar.bar('test 1'); 

现在我们得到输出:

 Inside bar function: test 1 

或者我们可以调用两次函数:

 fooBar.bar('test 1'); fooBar.bar('test 2'); 

现在我们得到输出:

 Inside bar function: test 1 Inside bar function: test 2 

下一步是创build一个函数,如你在s1.test()的调用中用作第二个参数的函数。 但不是将其分配给该参数,而是将其分配给另一个variables。

 // Create some object var fooBar = { foo: 'foo object'}; // Add a bar function to that object fooBar.bar = function (param1, param2) { console.log("Inside bar function: " + param1); }; // Create assign a function to a variable var func = function (err, result) { console.log("Inside func function, call using: " + err + " and " + result); } fooBar.bar('test 1'); fooBar.bar('test 2'); 

她将函数赋值给variablesfuncfunc只是另一个variables,具有types的function 。 JavaScript中的callback只是一个具有functiontypes的function 。 要看到这个运行:

 console.log(typeof func); 

输出是:

 function 

输出没有改变! 定义这个新的函数并将其作为callback函数赋给一个variables不会运行代码! 您需要调用该函数来运行代码。 所以我们开始调用这个函数,把最后两行改为四行:

 func('outside', 'call 1'); fooBar.bar('test 1'); fooBar.bar('test 2'); func('outside', 'call 2'); 

现在的输出是:

 Inside func function, call using: outside and call 1 Inside bar function: test 1 Inside bar function: test 2 Inside func function, call using: outside and call 2 

现在接近你的例子,我们把funcvariables作为第二个parameter passing给fooBar.bar()函数:

 func('outside', 'call 1'); fooBar.bar('test 1', func); fooBar.bar('test 2', func); func('outside', 'call 2'); 

输出保持:

 Inside func function, call using: outside and call 1 Inside bar function: test 1 Inside bar function: test 2 Inside func function, call using: outside and call 2 

为什么? 因为fooBar.bar()函数中的代码对第二个参数没有任何作用。 所以我们把这个function改成:

 // Add a bar function to that object fooBar.bar = function (param1, param2) { console.log("Inside bar function: " + param1); console.log("The type of param 2 is: " + typeof param2); }; 

结果输出是这样的::

 Inside func function, call using: outside and call 1 Inside bar function: test 1 The type of param 2 is: function Inside bar function: test 2 The type of param 2 is: function Inside func function, call using: outside and call 2 

这个输出显示param2的值是函数本身,而不是输出结果! 为了运行代码,我们所要做的就是使用param2作为函数,我们甚至可以不止一次地调用函数:

 // Add a bar function to that object fooBar.bar = function (param1, param2) { param2("inside bar 1", param1); console.log("Inside bar function: " + param1); param2("inside bar 2", param1); }; 

结果是:

 Inside func function, call using: outside and call 1 Inside func function, call using: inside bar 1 and test 1 Inside bar function: test 1 Inside func function, call using: inside bar 2 and test 1 Inside func function, call using: inside bar 1 and test 2 Inside bar function: test 2 Inside func function, call using: inside bar 2 and test 2 Inside func function, call using: outside and call 2 

现在是时候创build看起来像你的start.js的代码了。 而不是为func创build一个variablesfunc ,我们使用一个匿名函数,就像你的例子。 完整的代码变成:

 // Create some object var fooBar = { foo: 'foo object'}; // Add a bar function to that object fooBar.bar = function (param1, param2) { param2("inside bar 1", param1); console.log("Inside bar function: " + param1); param2("inside bar 2", param1); }; // fooBar.bar('test 2', func); // Use anonymous function instead of func fooBar.bar('test 3', function (err, result) { console.log("Inside anonymous function, call using: " + err + " and " + result); } ); 

输出现在变成:

 Inside anonymous function, call using: inside bar 1 and test 3 Inside bar function: test 3 Inside anonymous function, call using: inside bar 2 and test 3 

我们可以用另外一个函数向fooBar.bar添加另一个调用:

 fooBar.bar('test 4', function (err, result) { console.log("Inside another anonymous function, call using: " + err + " and " + result); } ); 

输出:

 Inside anonymous function, call using: inside bar 1 and test 3 Inside bar function: test 3 Inside anonymous function, call using: inside bar 2 and test 3 Inside another anonymous function, call using: inside bar 1 and test 4 Inside bar function: test 4 Inside another anonymous function, call using: inside bar 2 and test 4 

我希望这些例子能说明为什么你的start.js中的匿名函数永远不会被调用。

最后一个需要注意的事项是:创build一个匿名函数并将其赋值给variables并在javaScript中创build一个函数之间有区别吗? 那之间是:

 // Create assign a function to a variable var func = function (err, result) { console.log("Inside func function, call using: " + err + " and " + result); } 

和:

 function func (err, result) { console.log("Inside func function, call using: " + err + " and " + result); } 

答案是不! 都在全局堆栈中创build一个名称为func的指针指向相同的函数。 你甚至可以用另一个名字创build一个全局函数,并将funcvariables指向它。 这个例子和以前使用的例子一样:

 function funcy (err, result) { console.log("Inside func function, call using: " + err + " and " + result); } var func = funcy; 

换句话说:一个variables可以指向一个函数,就像一个variables可以指向一个string,一个整数或一个对象一样。 一个函数是“只”一个variables可以指向的types之一。

将此函数赋值给一个variables不会执行代码! 为了发生这种情况,你必须调用variables指向的函数。

你不叫你的callback。 callback函数只是作为函数的参数提供的正常函数。

您的代码不会调用callback。 您正试图为callback设置一个值。 固定版本的s1.js:

 module.exports.test=function test(msg,result){ result(null, "Hello "+msg); console.log("Inside s1.js - "+result); }; 

那样的话,你正在调用callback。 我们需要传入null因为您已经将callback函数定义为function(err, result) 。 它有两个参数,第一个是err我们没有运行错误,所以我们只传入null

请尝试下面的例子s1.js ,代码是自我解释,让我知道你是否仍然需要帮助。

 var defaultValues = module.exports defaultValues.GetApiURL = function () { var apiURL = "http://localhost:1000/api/" return apiURL; }