溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么用node.js實(shí)現(xiàn)簡(jiǎn)單的反向代理

發(fā)布時(shí)間:2021-04-23 11:24:32 來(lái)源:億速云 閱讀:143 作者:小新 欄目:web開(kāi)發(fā)

這篇文章給大家分享的是有關(guān)怎么用node.js實(shí)現(xiàn)簡(jiǎn)單的反向代理的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

js的作用是什么

1、能夠嵌入動(dòng)態(tài)文本于HTML頁(yè)面。2、對(duì)瀏覽器事件做出響應(yīng)。3、讀寫(xiě)HTML元素。4、在數(shù)據(jù)被提交到服務(wù)器之前驗(yàn)證數(shù)據(jù)。5、檢測(cè)訪(fǎng)客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。7、基于Node.js技術(shù)進(jìn)行服務(wù)器端編程。

代碼如下:

const http = require('http');
const url = require('url');
const querystring = require('querystring');


http.createServer(function(oreq, ores) {
  console.log("服務(wù)已開(kāi)啟");
  if (oreq) {
    if (oreq.url !== '/favicon.ico') {
      let content = '',
        postData = '';
      // 封裝獲取參數(shù)的方法
      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);
      };
      // 封裝發(fā)起http請(qǐng)求的方法
      function httpRequest(options, ores) {
        let datas = "";
        return http.request(options, function(res) {
          res.setEncoding('utf8');
          res.on('data', function(chunk) {
            // 返回?cái)?shù)據(jù)
            datas += chunk;
          });
          res.on('end', function() {
            ores.writeHead(200, {
              "Content-Type": "application/json; charset = UTF-8",
              "Access-Control-Allow-Origin": "*"
            });
            ores.end(datas);
          })
        })
      };
      // 數(shù)據(jù)塊接收中
      console.log(oreq.method.toUpperCase());
      if (oreq.method.toUpperCase() === "POST") {
        console.log("進(jìn)入POST");
        oreq.on("data", function(postDataChunk) {
          postData += postDataChunk;
        });
        // 數(shù)據(jù)接收完畢,執(zhí)行回調(diào)函數(shù)
        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"
          };
          // 發(fā)送請(qǐng)求
          let req = httpRequest(options, ores);
          req.on('error', function(e) {
            console.log('problem with request: ' + e.message);
          });
          req.write(postData); //發(fā)送請(qǐng)求數(shù)據(jù)
          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"
        };
        // 發(fā)送請(qǐng)求
        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');

感謝各位的閱讀!關(guān)于“怎么用node.js實(shí)現(xiàn)簡(jiǎn)單的反向代理”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI