Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Created July 6, 2017 16:23
Show Gist options
  • Save ccnokes/94576dc38225936a3ca892b989c9d0c6 to your computer and use it in GitHub Desktop.
Save ccnokes/94576dc38225936a3ca892b989c9d0c6 to your computer and use it in GitHub Desktop.
Good default configuration for axios in node.js
const axios = require('axios');
const http = require('http');
const https = require('https');
module.exports = axios.create({
//60 sec timeout
timeout: 60000,
//keepAlive pools and reuses TCP connections, so it's faster
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
//follow up to 10 HTTP 3xx redirects
maxRedirects: 10,
//cap the maximum content length we'll accept to 50MBs, just in case
maxContentLength: 50 * 1000 * 1000
});
//optionally add interceptors here...
@AbhieSpeaks
Copy link

Thanks.

@supertong
Copy link

My 2 cents here.
It's good that you enable KeepAlive feature of Http Agent, however, the default timeout behaviour for NodeJS built-in HttpAgent is a bit "impractical". Let me explain what that mean.
Since you are using KeepAlive, the socket is re-used across connections. The problem happens when remote service changes its IP address, which could happen during redeployment. The request starts triggering timeout because the underlying socket is now pointing to a IP which leads to nowhere. And, by default, HttpAgent do nothing about it. AFAIK, it's still true on version 8. https://nodejs.org/dist/latest-v8.x/docs/api/net.html#net_socket_settimeout_timeout_callback . The result of the problem is that all the requests keep timing-out and it's probably hard to find out what's wrong.
In our use-case, we use https://github.com/node-modules/agentkeepalive which actually handle that timeout event nicely.
Hope it helps.

@CoreyCole
Copy link

@supertong can you use agentkeepalive with axios? how?

@marciopd
Copy link

marciopd commented Apr 3, 2019

In order to use agentkeepalive in axios, you can pass its agents in request config properties httpAgentand httpsAgent.

Something like

const Agent = require('agentkeepalive');
const keepAliveAgent = new Agent({
  maxSockets: 100,
  maxFreeSockets: 10,
  timeout: 60000, // active socket keepalive for 60 seconds
  freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
});

const axiosInstance = axios.create({httpAgent: keepAliveAgent});
...

@oderayi
Copy link

oderayi commented Apr 30, 2019

Thank you!

These
... httpAgent: new http.Agent({ keepAlive: true }), httpsAgent: new https.Agent({ keepAlive: true }), ...

fixed the weird HPE_INVALID_CHUNK_SIZE error when talking to a proxied API server that was not properly set up.

@squalvj
Copy link

squalvj commented Nov 25, 2019

Hi, i would like to ask,
is this for NODE.JS server ? or i also can apply this method in react app ?

@ccnokes
Copy link
Author

ccnokes commented Nov 25, 2019

Hi, i would like to ask,
is this for NODE.JS server ? or i also can apply this method in react app ?

This is for node.js only.

@fourthdimension
Copy link

Hi, i would like to ask,
is this for NODE.JS server ? or i also can apply this method in react app ?

@squalvj: Did you ever find a solution for react app? If you could kindly share your solution, would be greatly appreciated!

@tombeynon
Copy link

Thank you @ccnokes and @supertong - solved some very frustrating issues thanks to your comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment