How to use the Agent function from https

Find comprehensive JavaScript https.Agent code examples handpicked from public code repositorys.

https.Agent is a module in Node.js that provides an HTTP(S) agent that allows making HTTPS requests to a server with SSL encryption.

1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
  http.close();
  logger.error(`本机${WEB_PORT}端口被其他应用程序占用,请尝试关闭占用${WEB_PORT}端口的其他程序 或 修改配置文件的 WEB_PORT 配置项。错误代码:${err.code}`.error);
  setTimeout(() => StartHttpServer(), 10000);
});


const UnauthorizedHttpsAgent = new https.Agent({ rejectUnauthorized: false }); // #303,Watt Toolkit(Steam++)的自签证书问题


/**
 * 检查本体更新
 */
fork icon19
star icon62
watch icon4

39
40
41
42
43
44
45
46
47
48
49
const {performance} = require('perf_hooks');


require('events').EventEmitter.defaultMaxListeners = Infinity;
const https = require('https');
const opts = {
    agent: new https.Agent({
        keepAlive: true
    })
};

fork icon2
star icon3
watch icon1

How does https.Agent work?

https.Agent is a module in Node.js that provides an HTTP(S) agent that allows making HTTPS requests to a server with SSL encryption. When creating an instance of https.Agent, you can pass an optional object as a configuration argument that specifies various properties of the agent. Some of the important properties are: keepAlive: A boolean that specifies whether the agent should use HTTP keep-alive to reuse TCP connections between requests. keepAliveMsecs: A number that specifies the number of milliseconds to wait before closing a TCP connection that has been idle. maxSockets: A number that specifies the maximum number of sockets to allow per host. maxFreeSockets: A number that specifies the maximum number of sockets to leave open before the agent starts closing them. The agent returned by https.Agent can be used as the agent property in an HTTPS request options object, which is passed to the https.request method to initiate an HTTPS request. The agent will handle SSL/TLS certificate validation and reusing the same connection for multiple requests if keepAlive is enabled. Here's an example of using https.Agent to make an HTTPS request with keep-alive enabled: javascript Copy code {{{{{{{ const https = require('https'); const agent = new https.Agent({ keepAlive: true }); const options = { hostname: 'www.example.com', path: '/', agent: agent }; https.request(options, (res) => { // handle response }); In this example, we first require the https module and then create a new instance of https.Agent with keepAlive set to true. We then create an options object that specifies the target hostname and path, as well as the agent to use for the request. Finally, we call https.request with the options object and a callback function to handle the response.

8
9
10
11
12
13
14
15
16
17
const start = new Date();
let status = true;
shell.echo(`
✨  Importing locales from "${apiUrl}"
`);
const agent = new https.Agent({
  rejectUnauthorized: false
});
let { data } = await axios.get(apiUrl, { httpsAgent: agent });
const bannedKeys = ['id', 'key', 'created_at', 'updated_at', 'published_at']; // useless-keys
fork icon1
star icon0
watch icon0

32
33
34
35
36
37
38
39
40
41
  headers, enableInsecure, acceptCodes, maxRedirects,
} = options;
const validCodes = acceptCodes && acceptCodes !== 'null' ? acceptCodes : null;
const startTime = new Date();
const requestMaker = axios.create({
  httpsAgent: new https.Agent({
    rejectUnauthorized: !enableInsecure,
  }),
});
requestMaker.request({
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const https = require("https");

const agent = new https.Agent({
  keepAlive: true,
});

const options = {
  hostname: "www.example.com",
  path: "/",
  agent: agent,
};

https.request(options, (res) => {
  // handle response
});

In this example, we first require the https module and then create a new instance of https.Agent with keepAlive set to true. We then create an options object that specifies the target hostname and path, as well as the agent to use for the request. Finally, we call https.request with the options object and a callback function to handle the response.

31
32
33
34
35
36
37
38
39
40
        ...headers,
        'Authorization':'xl-blog-next-app'
    }
}
if(process.env.NEXT_PUBLIC_IS_HTTPS==='TRUE') {
    options.agent = new https.Agent({
        rejectUnauthorized: false
    })
}

fork icon0
star icon0
watch icon0

70
71
72
73
74
75
76
77
78
79
  url: 'http://nchormichael-001-site1.atempurl.com/api/Student/1',
  headers: { 
    'Content-Type': 'application/json', 
  },
  responseType: 'json',
  httpsAgent: https.Agent({rejectUnauthorized: false})
};

axios(config)
.then(function (response) {
fork icon0
star icon0
watch icon0

+ 2 other calls in file

17
18
19
20
21
22
23
24
25
26
27
28
  res.writeHead(200);
  res.end('Hello, World!');
}));


server.listen(0, common.mustCall(() => {
  const agent = new https.Agent();
  const name = agent.getName({ port: server.address().port });
  https.globalAgent = agent;


  makeRequest();
fork icon0
star icon0
watch icon0