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
1 Answer
Reset to default 6I 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条)