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

首頁 > 編程 > JavaScript > 正文

詳解用node.js實現簡單的反向代理

2019-11-19 16:14:58
字體:
來源:轉載
供稿:網友

之前用node.js實現簡單的反向代理,最近需要回顧,就順便發到隨筆上了

不多說直接上代碼!

const http = require('http');const url = require('url');const querystring = require('querystring');http.createServer(function(oreq, ores) {  console.log("服務已開啟");  if (oreq) {    if (oreq.url !== '/favicon.ico') {      let content = '',        postData = '';      // 封裝獲取參數的方法      function getParmas(oUrl) {        let oQuery = (typeof oUrl === "object") ? oUrl : url.parse(oUrl, true).query,          data = {};        for (item in oQuery) {          if (item !== 'hostname') {            if (item !== 'path') {              data[item] = oQuery[item];            }          }        }        return querystring.stringify(data);      };      // 封裝發起http請求的方法      function httpRequest(options, ores) {        let datas = "";        return http.request(options, function(res) {          res.setEncoding('utf8');          res.on('data', function(chunk) {            // 返回數據            datas += chunk;          });          res.on('end', function() {            ores.writeHead(200, {              "Content-Type": "application/json; charset = UTF-8",              "Access-Control-Allow-Origin": "*"            });            ores.end(datas);          })        })      };      // 數據塊接收中      console.log(oreq.method.toUpperCase());      if (oreq.method.toUpperCase() === "POST") {        console.log("進入POST");        oreq.on("data", function(postDataChunk) {          postData += postDataChunk;        });        // 數據接收完畢,執行回調函數        oreq.on("end", function() {          console.log("接收完畢")          console.log(postData);            // 配置options          let oData = JSON.parse(postData);          postData = getParmas(oData);          let options = {            hostname: oData.hostname,            port: '80',            path: oData.path,            method: "POST"          };          // 發送請求          let req = httpRequest(options, ores);          req.on('error', function(e) {            console.log('problem with request: ' + e.message);          });          req.write(postData); //發送請求數據          req.end();        });      } else {        let queryObj = url.parse(oreq.url, true).query;        content = getParmas(oreq.url);        let options = {          hostname: queryObj.hostname,          port: '80',          path: queryObj.path + content,          method: "GET"        };        // 發送請求        let req = httpRequest(options, ores);        req.on('error', function(e) {          console.log('problem with request: ' + e.message);        });        req.end();      }    }  }}).listen(8080, '127.0.0.1');

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛教| 丰宁| 杭州市| 和顺县| 静安区| 左云县| 盐源县| 页游| 和林格尔县| 化德县| 双城市| 佛冈县| 桂阳县| 定西市| 秭归县| 大丰市| 甘泉县| 外汇| 高青县| 玛纳斯县| 新化县| 汽车| 三门县| 丘北县| 古蔺县| 中超| 宜良县| 理塘县| 东乌珠穆沁旗| 汝城县| 肇州县| 永宁县| 福泉市| 潜山县| 日喀则市| 安阳县| 攀枝花市| 永善县| 沾化县| 蒙阴县| 鸡西市|