node.js - 请求:使用请求 node.js 两次调用响应函数获取请求

标签 node.js firebase http request google-cloud-functions

我正在编写一个攻击 API 的 Firebase 云函数,获取一些数据并在序列化后将数据上传到我的 Firestore 数据库。这是使用 Cron 安排的,每天执行一次。我的功能工作正常,一切正常。但是我的 http 请求调用了成功函数两次。

我正在使用 request用于管理 HTTP 请愿书。我的代码如下所示:

return request({
    uri: uri,
    method: "GET",
    qs: propertiesObject,
    json: true
}, function (error, response, body) {
    if (response.statusCode == 200) {
        return serializeResponse(body, region, bracket, firebaseResponse)
    } else {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
        return firebaseResponse.status(400).send("The petition to Battle.NET API has failed");
    }
});

方法 serializeResponse 只是序列化给定的数据并将其上传到 Firestore,没有别的。 FirebaseResponse 是从 http 请求到需要关闭的云函数的原始响应,发送成功或错误代码以避免执行的无限循环。

但是,问题是 function 被调用了两次,我不明白为什么。在我向代码添加愚蠢的 bool 控件或丑陋的东西之前,我宁愿寻求帮助。

最佳答案

因为每次请求一个页面浏览器默认是favicon.ico,做个判断

var pathname = url.parse(request.url).pathname;    
if(pathname != '/favicon.ico'){
}

关于node.js - 请求:使用请求 node.js 两次调用响应函数获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358118/

相关文章:

不使用 netsh 注册 URI 的 C# HttpListener

html - 错误的 URI Ruby on Rails : is it because long?

node.js - 使用nodejs和mongoDB作为后端数据进行延迟加载

node.js - Mocha 未在子文件夹中获取测试

javascript - 无法部署谷歌云功能(错误 : ELIFECYCLE)

java - 从火力基地得到 child ?

angular - 为什么这个 Angular 应用程序无法处理用户登录后更改显示内容的用户状态(登录\注销)?

javascript - 使用ng-repeat、ng-form、ng-submit后如何提交

ios - 在 firebase 中获取个人用户详细信息

python - HTTP 303 (SeeOther) : GET Works, POST 失败