node.js - 在 Firebase 云功能中出现“getaddrinfo ENOTFOUND”错误
问题描述
我在 Firebase 云函数中编写了一个函数来向 Onesignal API 发送 HTTP Post。我首先在 Postman 中测试了我的请求,并确保正文和标题正确且有效 - 它成功了。然后我继续使用request npm package创建一个发布请求。这是我到目前为止所拥有的
exports.addMessage = functions.https.onRequest((req, res) => {
const Requestbody = {
included_segments: ['Subscribed Users'],
app_id: 'XXXXXXXXXX',
contents: { 'en': 'Test Notification Body' },
headings: { 'en': 'Test Title' }
}
httpRequest.post({
url: 'https://onesignal.com/api/v1/notifications',
headers: {
'Authorization': 'Some Token...',
'Content-Type': 'application/json'
},
body: JSON.stringify(Requestbody)
},
function (error, response, body) {
if (error) { return res.status(500).send('Failed - ' + JSON.stringify(error)); }
console.log('Onesignal Response: ' + JSON.stringify(response));
return res.status(200).send('Success');
});
}
每次我调用此云函数时,request.post 调用都会返回以下错误:
getaddrinfo ENOTFOUND onesignal.com onesignal.com:443
但我提供的那个 URL 正是我在 Post Man 中用来测试的那个。
解决方案
正如 Doug Stevenson 和 LundinCast 所指出的,我的 Firebase 计划不支持对外部主机的调用。我需要升级到至少Flame
支持它的计划。
推荐阅读
- yii2 - 路由部分的 Yii2 规则
- javascript - Array.fill() 保留先前选择的月份日期
- javascript - 在nodejs中将数组转换为json格式
- reactjs - 如何使用 webpack 和 react 使用不同项目常见的节点模块依赖项
- php - Xampp 中的 php 5.6 中的多线程
- vhdl - 执行期间 VHDL 复位
- c# - 如何发布到 RESTful Web API
- amazon-web-services - AWS:Amazon Cognito 与 STS 和 SAML
- reactjs - 打字稿导入模块问题
- dart - 演练中的下一个和上一个按钮