using a custom http.Agent (extend documentation)
I was searching for a solution to use a custom http.Agent for every request. In early 3.x releases, it was possible to do something like (ref: #1130):
const http = require('http')
const request = require('superagent')
const myAgent = new http.Agent()
request(method, url).agent(myAgent)
With the PR #1302 the .agent() method only accepts some options, but no http.Agent instance.
My solution was to inject a middleware function with .use(...).
Is there any better solution for this scenario?
I would suggest to extend the documentation about using custom http.Agent and superagent.agent().use().
My solution:
const myAgent = new http.Agent()
const request = superagent.agent().use(req => req.agent(myAgent))
@niftylettuce, could you please clarify what is expected/good way to set custom agent?
My use-case: I would like to reuse connections by using agent with keepAlive: true - see `https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_new_agent_options
I would love to know what the proper way is to use keep alive with superagent also. I used stixx200's solution but there's got to be a cleaner way to do it.
Same here. I would love to have details about connection reuse, keepAlive, and how to use à custom Agent instance.