1

My application is in react and calls the node layer via axios call. The node layer then calls an external api which takes around 7 mins to respond. However i get a timeout error in about 2-3 mins from the time the request was made.When i call the external api directly (not via node layer ) i am able to get the response in 7 mins. I have tried setting timeout for node using the suggestions like

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);

and also using server.timeout but nothing seems to work. Can someone suggest on how to resolve the issue.

I am getting the following error

Network Error at createError (webpack-internal:///./node_modules/axios/lib/core/createError.js:16:15

I am using 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})
  })
Bhavika
  • 540
  • 1
  • 7
  • 16
  • Timeout is in millis so `10000` is just 10s, try increasing like `420000` (7 min) https://stackoverflow.com/questions/23925284/how-to-modify-the-nodejs-request-default-timeout-time – 1565986223 Apr 18 '19 at 11:29
  • I have tried with timeout set to 200000000000. It isn't working with that. – Bhavika Apr 18 '19 at 11:46

2 Answers2

0

There is timeout for both node as well as axios

1) Setting timeout for node

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) Setting timeout for axios

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 10000, // in milliseconds
  headers: {'X-Custom-Header': 'foobar'}
});
tarzen chugh
  • 10,561
  • 4
  • 20
  • 30
0

I think you're calling setTimeOut in the wrong place. You might need to set createServer in a var, and then call setTimeOut on it as follows:

`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);

Hope this helps

Maffekill
  • 72
  • 6