【发布时间】:2012-10-20 11:00:21
【问题描述】:
为了学习 node.js,我正在创建一个小应用程序,它获取一些存储在 mongoDB 中的 rss 提要,处理它们并从这些提要中创建一个提要(按日期排序)。
它会解析约 50 个 rss 提要的列表,其中包含约 1000 个博客项目,因此解析整个内容的时间很长,因此我输入了以下 req.connection.setTimeout(60*1000); 以获得足够长的时间来获取和解析所有提要.
一切运行良好,但请求被调用了两次。 (我用wireshark查过,我不认为这与favicon有关)。
我真的不明白。
您可以在这里测试自己:http://mighty-springs-9162.herokuapp.com/feed/mde/20(它应该创建一个包含最近 20 篇关于“mde”的文章的 rss 提要)。
代码在这里:https://github.com/xseignard/rss-unify
如果我们专注于有趣的部分:
我有一个这样定义的路线:app.get('/feed/:name/:size?', topics.getFeed);
而topics.getFeed 是这样的:
function getFeed(req, res) {
// 1 minute timeout to get enough time for the request to be processed
req.connection.setTimeout(60*1000);
var name = req.params.name;
var callback = function(err, topic) {
// if the topic has been found
if (topic) {
// aggregate the corresponding feeds
rssAggregator.aggregate(topic, function(err, rssFeed) {
if (err) {
res.status(500).send({error: 'Error while creating feed'});
}
else {
res.send(rssFeed);
}
},
req);
}
else {
res.status(404).send({error: 'Topic not found'});
}};
// look for the topic in the db
findTopicByName(name, callback);
}
所以没什么特别的,但是这个 getFeed 函数还是被调用了两次。
那里有什么问题?有什么想法吗?
【问题讨论】:
-
最常见的问题是你忘记使用
return -
您好 mvbl-fst,我不明白您忘记
return的意思。你能解释更多吗? -
我可能对上述内容有误,在这段代码中,没有什么能让它执行两次(favicon.ico 除外,但你说这是不可能的)。我唯一要做的就是做
return findTopicByName(name, callback);。但很确定它不会有帮助。 -
你能把它添加到 git 中,这样我就可以拉出来看看发生了什么?
-
所以我做了更多的调查,它似乎只发生在 Chromium(和我认为是 Chrome)上,而在 firefox 上不会发生。