javascript - Enabling 'keepAlive' functionality in Axios and NodeJS - Stack Overflow

I have read a bunch of documentation, Stack Overflow posts, and various blog posts and can't seem

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

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)});
}
Share Improve this question asked Mar 11, 2022 at 16:43 puterDudeputerDude 1011 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 :)!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744124509a4559555.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信