我有以下两个文件,无法从模块请求中获取结果到var内部app.js
。
我将module.exports
导出视为回调,但找不到正确的组合。
// app.js
#!/usr/bin/env node
// i am a nodejs app
var Myobject = require('./code.js');
var value1 = "http://google.com";
var results = Myobject(value1); // results should stare the results_of_request var value
console.dir(results); // results should stare the results_of_request var value
现在出现模块// code.js
// i am a nodejs module
module.exports = function(get_this) {
var request = require('request');
var options = {
url: get_this,
};
request(options, function(error, response, body) {
if (!error) {
// we got no error and request is finished lets set a var
var result_of_function = '{"json":"string"}'
}
}
// the main problem is i have no way to get the result_of_function value inside app.js
}
由于从模块导出的函数是异步的,因此您需要从应用程序中通过回调处理其结果在应用程序中:
Myobject(value1, function(err, results){
//results== '{"json":"string"}'
});
在您的模块中:
module.exports = function(get_this, cbk) {
var request = require('request');
var options = {
url: get_this,
};
request(options, function(error, response, body) {
if (error) {
return cbk(error);
}
// we got no error and request is finished lets set a var
var result_of_function = '{"json":"string"}'
return cbk(null, result_of_function)
}
// the main problem is i have no way to get the result_of_function value inside app.js
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句