-
-
Save ccnokes/94576dc38225936a3ca892b989c9d0c6 to your computer and use it in GitHub Desktop.
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... |
In order to use agentkeepalive
in axios, you can pass its agents in request config properties httpAgent
and 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});
...
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.
Hi, i would like to ask,
is this for NODE.JS server ? or i also can apply this method in react app ?
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.
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!
Thank you @ccnokes and @supertong - solved some very frustrating issues thanks to your comments!
Please note that since Node.js 19.0.0, http.globalAgent
enables keepAlive
by default, which means that in Axios, when no httpAgent
parameter is passed, keepAlive
is also enabled by default.
@supertong can you use
agentkeepalive
with axios? how?