Welcome to the Comprehensive Guide to hpagent for Enhanced HTTP Performance Optimization
The hpagent
module is designed to optimize the HTTP requests, significantly improving the performance and reliability of Node.js applications by providing fine-grained control over HTTP/HTTPS agents. In this comprehensive guide, we will cover its various APIs with detailed explanations and examples.
Installation
npm install hpagent
Creating an HTTPS Agent
const { HttpsProxyAgent } = require('hpagent'); const agent = new HttpsProxyAgent({ keepAlive: true, keepAliveMsecs: 1000, maxSockets: 256, maxFreeSockets: 256, scheduling: 'lifo', proxy: 'http://localhost:8080' }); // Use the agent with https const https = require('https'); https.get('https://example.com', { agent }, (res) => { // Handle response });
Creating an HTTP Agent
const { HttpProxyAgent } = require('hpagent'); const agent = new HttpProxyAgent({ keepAlive: true, keepAliveMsecs: 1000, maxSockets: 256, maxFreeSockets: 256, scheduling: 'lifo', proxy: 'http://localhost:8080' }); // Use the agent with http const http = require('http'); http.get('http://example.com', { agent }, (res) => { // Handle response });
Manual Proxy Settings
It is possible to manually define proxy settings for finer control over network traffic.
const agent = new HttpsProxyAgent({ proxy: { protocol: 'http', host: '10.0.0.1', port: 8080, auth: { username: 'user', password: 'password' } } }); // Use the agent with https const https = require('https'); https.get('https://example.com', { agent }, (res) => { // Handle response });
Using the Agent with Axios
Integrating hpagent with axios is straightforward and adds a significant boost to performance.
const axios = require('axios'); const { HttpProxyAgent } = require('hpagent'); const agent = new HttpProxyAgent({ keepAlive: true, proxy: 'http://localhost:8080' }); axios.get('http://example.com', { httpAgent: agent }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });
Full Application Example
A simple application utilizing the hpagent for making HTTP and HTTPS requests.
const express = require('express'); const axios = require('axios'); const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent'); const app = express(); const httpAgent = new HttpProxyAgent({ keepAlive: true, proxy: 'http://localhost:8080' }); const httpsAgent = new HttpsProxyAgent({ keepAlive: true, proxy: 'http://localhost:8080' }); app.get('/fetch-http', (req, res) => { axios.get('http://example.com', { httpAgent }) .then(response => { res.send(response.data); }) .catch(error => { res.status(500).send(error.toString()); }); }); app.get('/fetch-https', (req, res) => { axios.get('https://example.com', { httpsAgent }) .then(response => { res.send(response.data); }) .catch(error => { res.status(500).send(error.toString()); }); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });
Use the examples above to integrate hpagent
into your applications efficiently. Happy coding!
Hash: c0bb5f22b3e858ae33bc1ef00b90ed65901ea85681ede436ae680b1fcc8eedcf