nodejs实现的http、https 请求封装操作示例
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了nodejs实现的http、https 请求封装操作,结合实例形式分析了node.js针对http、https 请求的封装与使用相关操作技巧,需要的朋友可以参考下
本文实例讲述了nodejs实现的http、https 请求封装操作。分享给大家供大家参考,具体如下
libs/request.js
const URL = require('url'); const zlib = require('zlib'); const http = require('http'); const https = require('https'); const qs = require('querystring'); function Request(cookie) { this.cookies = []; if (cookie !== undefined) { this.setCookie(cookie); } } Request.prototype.getHeaders = function(host, postData) { let headers = { 'Host': host, 'Pragma': 'no-cache', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Content-Type': 'application/x--form-urlencoded', 'Aept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2', 'Aept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8', 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1', }; if (this.cookies.length) { headers.Cookie = this.cookies.join('; '); } if (postData != '') { headers['Content-Length'] = Buffer.byteLength(postData); } return headers; } Request.prototype.setCookie = function(cookie) { let cookies = cookie.split(';'); for (let c of cookies) { c = c.replace(/^\s/, ''); this.cookies.push(c); } return this; } Request.prototype.request = function(method, url, params) { let postData = qs.stringify(params || {}); let urlObj = URL.parse(url); let protocol = urlObj.protocol; let options = { hostname: urlObj.host, port: urlObj.port, path: urlObj.path, method: method, headers: this.getHeaders(urlObj.host, postData), }; return new Promise((resolve, reject) => { let req = (protocol == 'http:' ? http : https).request(options, (res) => { let chunks = []; res.on('data', (data) => { chunks.push(data); }); res.on('end', () => { let buffer = Buffer.concat(chunks); let encoding = res.headers['content-encoding']; if (encoding == 'gzip') { zlib.gunzip(buffer, function(err, decoded) { resolve(decoded.toString()); }); } else if (encoding == 'deflate') { zlib.inflate(buffer, function(err, decoded) { resolve(decoded.toString()); }); } else { resolve(buffer.toString()); } }); }); req.on('error', (e) => { reject(e); }); if (postData != '') { req.write(postData); } req.end(); }) } Request.prototype.get = function(url) { return this.request('GET', url, null); } Request.prototype.post = function(url, params) { return this.request('POST', url, params); } module.exports = function(cookie) { return new Request(cookie); }
test.js
const request = require('./request')(); (async function() { let res = await request.get('http://.axita../'); console.log(res); })();
执行命令
nodemon test.js
希望本文所述对大家node.js程序设计有所帮助。
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南