簡體   English   中英

axios post 請求掛在有效請求上

[英]axios post request hangs on valid requests

我在我的 nodejs 應用程序中使用 axios 將請求發送到另一個服務。
當我發送請求並得到 5xx 或 4xx 錯誤代碼作為響應時 - axios 會很好地處理請求並將錯誤返回給客戶端。
當請求結構良好時,它只是掛起並且 axios 發送的承諾永遠不會解析,讓客戶端無限期地掛起等待響應。
我不知道是什么導致只有有效請求掛起,也不知道如何進一步調試。
到目前為止我嘗試過的事情 -

  • 使用郵遞員發送請求(失敗和有效) - 它們按預期返回錯誤代碼和 OK 代碼。
  • 在VScode中調試節點進程,我可以看到axios在內部無限循環,等待promise解決,但我還沒有理解是什么阻止promise解決。
  • axios 版本有點過時,所以我嘗試更新它,但這產生了新問題,所以我決定將其撥回原始版本。

如果相關,有關應用程序版本的一些信息 -

  • 節點版本 - v9.11.1
  • axios 版本 - 0.9.1

axios 請求的片段,出於安全原因進行了編輯 -

 const Router = require('express').Router; const router = Router(); const axios = require('axios').create({ baseURL: 'https://some-service.com' }); const auth-handler = require('auth-handler'); router.post('/', (req, res) => { auth-handler.getToken('token-name') .then(token => { const body = { subject: "subject", body: "body" }; const options = { headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' } }; return axios.post('/'+req.body.route+'/endpoint?foo=bar', body, options); }) .then(response => res.status(response.status).send(response.data)) .catch(error => { console.error(error); res.status(error.status).send(error.data); }); });

面臨類似的問題。 Axios 會卡在實例(請求)步驟中。 在 URL 中用http替換https解決了這個問題。

對此的答案是 axios 一直在按預期解決承諾,只是在我的程序的客戶端沒有任何指示,只有錯誤在表單中正確指示。

我已經添加了 2xx 響應的指示,現在一切又好了。

對我來說,它在使用https://www.npmjs.com/package/axios-request-throttle后起作用了

static myFynction(urls): void {
    axiosThrottle.use(axios, {
        requestsPerSecond: 2
    });
    const getUrls = [];
    urls.forEach((url: string) => {
    const axiosCall = axios({
            method: 'GET',
            headers: {
                ...
            },
            url: url,
        })
        .then((response) => {
            return response.status;
        })
        .catch((error) => {
            return error.message;
        });
    getUrls.push(axiosCall);
});
const responses = browser.call(async () => await Promise.all(getUrls));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM