国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

nodejs實(shí)現(xiàn)的http、https 請(qǐng)求封裝操作示例

2024-05-06 15:44:10
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了nodejs實(shí)現(xiàn)的http、https 請(qǐng)求封裝操作。分享給大家供大家參考,具體如下:

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-www-form-urlencoded', 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2', 'Accept': '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://www.axita.com.cn/'); console.log(res);})();

執(zhí)行命令

nodemon test.js

希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 民县| 湖州市| 金昌市| 融水| 华蓥市| 桐城市| 小金县| 洛宁县| 新乡市| 宁晋县| 镇赉县| 高唐县| 南阳市| 班戈县| 夹江县| 犍为县| 上杭县| 景德镇市| 昆明市| 信阳市| 望奎县| 辽源市| 西乡县| 梧州市| 仪征市| 万盛区| 新营市| 平潭县| 章丘市| 余干县| 集贤县| 息烽县| 中宁县| 海淀区| 射阳县| 那曲县| 河曲县| 当阳市| 富源县| 汉源县| 麻栗坡县|