Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

549
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda