0
我正在写一个Node.js应用程序,我无法返回我的网页刮到我的主要app.get函数的值。该页面被抓取得很好,并且它的结果使我一直回到我的回调中的RETURN,但它实际上并没有返回值。任何帮助,将不胜感激!如何返回只有当我的回调完成
编辑:这必须是一个纯JavaScript的解决方案,不使用jQuery的。
在我server.js文件,我有这样的代码:
var machineDetails = helpers.scrapePage();
app.get('/', function (req, res) {
res.render('index', { machineDetails: machineDetails, title: 'VIP IT Dashboard'});
});
在helpers.js文件我有以下功能
//Requires
httpntlm = require('httpntlm'),
cheerio = require('cheerio');
var userData;
function callSite(callback) {
//Scrape site and get information
var scrape;
httpntlm.get({
url: "http://URLthatIamScraping.com",
username: 'username1',
password: 'password1',
domain: 'companyDomain'
}, function (err, res) {
if (err) return err;
//Sort information for the computer
var $ = cheerio.load(res.body);
var scrape = $('#assetcontent').html();
//Return the html content of the page
callback(scrape);
});
}
exports.scrapePage = function(){
return callSite(function(data) {
//This is called after HTTP request finishes
userData = data;
//ISSUE: userData is not actually making it back to my server.js variable called "machineDetails"
return userData;
});
}
好极了!我将'helper.scrapePage'移到了app.get之外,因此每次有人访问''/''时都不会调用它。非常感谢! – RandomDeduction 2014-10-29 16:17:27