簡體   English   中英

節點js請求超時問題

[英]Node js request timeout issue

我的應用程序處於反應狀態並通過axios調用調用node層。 然后, node層調用外部api ,大約需要7分鍾來響應。 但是,我在請求發出后大約2-3分鍾內出現超時錯誤。當我直接調用外部api (而不是通過節點層)時,我能夠在7分鍾內得到響應。 我已嘗試使用類似的建議為node設置timeout

var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end("Hello World");
    }).on('connection', function(socket) {
      socket.setTimeout(200000000);
    }).listen(3000);

並且還使用server.timeout但似乎沒有任何工作。 有人可以建議如何解決問題。

我收到以下錯誤

createError處的網絡錯誤(webpack-internal:///./node_modules/axios/lib/core/createError.js:16:15

我正在使用axios

axios.post('/api/parseAndExtractFile', formData, config)
  .then((response) => {
    if (response.status === 200) {
      const fileName = `enriched_v3_spec_${new Date().getTime()}`
      const blob = new Blob([(response.data)], {type: 'application/vnd.ms-excel.sheet.macroEnabled.12'})
      saveAs(blob, fileName)
    } else {
      toast.error('Oops, something went wrong. Please try again.', {autoClose: 4000})
    }
    this.setState({
      loading: false,
      showAuditLog: 'show'
    })
  })
  .catch((error) => {
    this.setState({loading: false})
    toast.error(`Error while fetching data ${error}`, {autoClose: 4000})
  })

節點和axios都有超時

1)設置節點的超時

var http = require('http');
var srvr = http.createServer(function (req, res) {
  res.write('Hello World!');
  res.end();
});
srvr.listen(8080);
srvr.timeout = 20000; // in milliseconds

2)設置axios的超時

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 10000, // in milliseconds
  headers: {'X-Custom-Header': 'foobar'}
});

我想你在錯誤的地方調用setTimeOut。 您可能需要在var中設置createServer,然后在其上調用setTimeOut,如下所示:

`var http = require('http');
var srv = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("Hello World");
})

svr.listen(3000);
svr.setTimeout(10000);

希望這可以幫助

暫無
暫無

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

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