Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

547
Views
Enabling 'keepAlive' functionality in Axios and NodeJS

I have read a bunch of documentation, Stack Overflow posts, and various blog posts and can't seem to get 'keepAlive' functionality to work. What am I missing here?

My server:

import express from "express";
var app = express();
var server = app.listen(3000);
var connectionCount = 1;
var requestCount = 1;

server.keepAliveTimeout = (60 * 1000) + 1000;
server.headersTimeout = (60 * 1000) + 2000;

server.on('connection', function(socket) {
  console.log(`A new connection (${connectionCount}) was made by a client.`);
  connectionCount++;
  socket.setTimeout(30 * 1000); 
});
server.on('request', (request, response) => {
      console.log(`New request #${requestCount}!!`);
      requestCount++;
    });
app.get('/', (req, res) => {
  res.send('Hello World, from express');
});

My script sending requests (new connection every time when I want 'keepAlive' so it's one connection for these requests):

import axios from 'axios';
import http from 'http';
import https from 'https';

const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });
const axiosInstance = axios.create();
for (let i = 1; i < 51; i++) { 
  axiosInstance.get(
    'http://localhost:3000',
    {
      httpAgent: httpAgent,
      httpsAgent: httpsAgent
    }
  )
  .then(function (response) {
    console.log(response.data);
    console.log(`GET ${i}`)
  })
  .catch(e => {console.error(e)});
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I figured it out. I "simply" needed an await before my call.

await axiosInstance.get()

You need to be using a newer version of Node (I am using 14.18.1) for this to work otherwise you need to wrap it all in an async function.

Hopefully my struggle helps someone else :)!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!