Bhavika
Bhavika

Reputation: 560

Node js request timeout issue

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

Upvotes: 1

Views: 10898

Answers (2)

Maffekill
Maffekill

Reputation: 72

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

Upvotes: 0

tarzen chugh
tarzen chugh

Reputation: 11257

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

Upvotes: 0

Related Questions