我在node.js中构建了一个Twitter机器人游戏。我有一个具有同步功能的脚本(打开Twitter流的streamOn
),然后是具有HTTP GET请求功能的async.waterfall
(getImage
)。当脚本被触发时,getImage
有时会返回301响应。 streamOn
继续保持打开,但玩家不知道错误,只是没有图像返回。如果抛出错误,我该如何重新启动async.waterfall?
如何让async.waterfall
从其开始的步骤“重试”,如果它抛出一个错误,所以玩家仍然得到一个图像?
这里是async.waterfall
:
streamOn(function(tweet) {
async.waterfall([
getName,
searchImage,
getImage,
postTweet.bind(null, tweet)
],
function(error, result) {
if (error) {
console.error(error);
return;
}
console.log(result);
});
}
这里是getImage
功能的简化版本:
function getImage() {
var http=require('http'), imageBuffer;
http.get(
'http://www.kame.net/img/kame-anime-small.gif',
function(res) {
var body=new Buffer(0);
if (res.statusCode!==200) {
return console.error('HTTP '+res.statusCode);
}
res.on('data', function(chunk) {
body=Buffer.concat([body, chunk]);
});
res.on('end', function() {
imageBuffer=body;
});
res.on('error', function(err) {
console.error(err);
});
}
);
}
严格来说,301不是错误。它告诉你你的网址需要更新。你有没有研究过你为什么得到重定向响应? – mdickin
@mdickin是的,谢谢澄清。图片来源来自Bing搜索结果,使用API的次数超过了它的效果,但是每隔一段时间它就会返回一个301响应。我真的需要运行脚本一段时间,然后它返回一个301 ... – filmplane
你有没有考虑重新运行GET请求对响应中的“位置”标头值?这应该告诉你图像已经移动的位置。 – mdickin