I'm running a node.js server A which uses superagent to issue HTTP requests to another server B.
I investigated the request on server B and saw the the header connection
being close
and the httpVersion being 1.1
:
var http = require('http');
var request = require('superagent');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('req.httpVersion seen on server:' + req.httpVersion);
res.write('\nreq.headers.connection seen on server:' + req.headers.connection);
res.end();
}).listen(1337, '0.0.0.0');
request
.get('localhost:1337/helloword')
.end(function (err, res) {
console.log(res.text);
});
This leads to:
req.httpVersion seen on server:1.1
req.headers.connection seen on server:close
However if I access the same server from a browser I get:
req.httpVersion seen on server:1.1
req.headers.connection seen on server:keep-alive
From https://www.rfc-editor.org/rfc/rfc2616#page-172 I learned that keep-alive
is the default for HTTP 1.1 unless declared otherwise by using Connection: close
.
So, my questions are:
SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node.
Here is a basic code: const superagent = require('superagent'); superagent. get('https://api.arabam.com/pp/step') . query({ apikey: '_V85Kref7xGZHc1XRpUmOhDDd07zhZTOvUSIbJe_sSNHSDV79EjODA==' }) .
In AWS Lambda for Node Js, there's a special environment variable called AWS_NODEJS_CONNECTION_REUSE_ENABLED. If you set this variable to 1, the lambda will keep the TCP connection alive for any AWS Service related HTTP request.
The Agent manages connection persistence for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty. After that, the socket is destroyed, if the keepAlive is set to false .
It doesn't seem to be documented but you can pass an http agent to superagent with the function agent
.
So you could create a keep-alive agent with this module: https://www.npmjs.org/package/agentkeepalive and pass it to superagent.
Something like this:
util = require('util');
util.debuglog = require('debuglog');
var http = require('http');
var request = require('superagent');
var Agent = require('agentkeepalive');
var keepaliveAgent = new Agent({
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000,
keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
});
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('req.httpVersion seen on server:' + req.httpVersion);
res.write('\nreq.headers.connection seen on server:' + req.headers.connection);
res.end();
}).listen(1337, '0.0.0.0');
request
.get('localhost:1337/helloword')
.agent(keepaliveAgent)
.end(function (err, res) {
console.log(res.text);
});
Browers can reuse socket handle, so it send the header Connection: keep-alive
to server.
If you want to keep alive connection you can send that header like this:
request
.get('localhost:1337/helloword')
.set('Connection', 'keep-alive')
.end(function (err, res) {
console.log(res.text);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With