【发布时间】:2020-11-20 23:43:27
【问题描述】:
我是 Nodejs 新手,如有任何错误请见谅.. :)
让我解释一下我要做什么:
基本上我正在为我的平台制作推送通知服务..我会进一步解释.. 我有两个 NodeJs 服务器(使用 express):
服务器 1: 它从数据库中获取所需的所有内容,例如(设备注册、标识符 ..),并应发送到第二个服务器。
SERVER 2:此服务器接收 JSON(包含我需要的所有内容)以创建 FCM 和 APNS 有效负载,然后发送到方便的提供程序(FCM、APNS)。
我正在使用什么:我正在使用 axios 发送 POST 请求。
问题:由于第一台服务器会同时发送大量请求(通常是 5K 或更多——它是动态的),所以 axios 无法处理,我已经尝试了很多axios 的其他替代品,但面临同样的问题。
我的问题:我怎样才能在没有任何问题的情况下发送这么多的请求?
PS:当我发送少量请求(100 个或更多)时,我不会遇到任何错误...
我希望一切都清楚,我非常感谢任何帮助。
Axios 请求代码示例:
PS:它总是落在“[Request Error] ...”中
try
{
axios.post(endpoint,{sendPushRequest})
.then( response => {
console.log(response.data);
})
.catch( er => {
if (er.response) {
console.log("[Response Error] on sending to Dispatcher...");
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(er.response.data);
console.log(er.response.status);
console.log(er.response.headers);
} else if (er.request) {
console.log("[Request Error] on sending to Dispatcher...");
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
} else {
// Something happened in setting up the request that triggered an Error
console.log('[Error]', er.message);
}
console.log(er.config);
});
}
catch (e) {
console.log('[ Catch Error]', e);
}
【问题讨论】:
-
嗯,我相当有信心 Node 和 Axios 可以同时发送 5k 个请求。另一方面,如果所有请求都发送到同一台服务器,则该服务器可能会感到被炸毁并进入 DDoS 保护模式以避免崩溃......任何原因为什么您绝对需要同时发送 5k 个请求?另外Axios的错误信息和失败原因是什么?
-
另外,最近我在使用 Axios 时遇到了很多麻烦。显然有an old known and yet unfixed bug 使一些请求永远随机挂起而不会引发错误。我摆脱了 Axios 并开始使用 node-fetch,它就像一个魅力。
-
好吧,为什么我需要发送 5k ?因为该平台是客户使用的,当他创建一个活动发送给他的用户时,我应该在短时间内尽可能多地发送。我将发布 AXIOS 错误消息(它很长,但我会发布它)
-
所以你正在向 5k 个不同的服务器发出 5k 个 POST 请求?即便如此,为什么您需要所有请求同时?你一次买不起 100 个吗?
-
不,我有两台服务器,一台是请求发送方,第二台是接收方
标签: javascript node.js server node-modules