广告
返回顶部
首页 > 资讯 > 前端开发 > node.js >详解用node.js实现简单的反向代理
  • 845
分享到

详解用node.js实现简单的反向代理

详解简单node 2022-06-04 17:06:42 845人浏览 安东尼
摘要

之前用node.js实现简单的反向代理,最近需要回顾,就顺便发到随笔上了 不多说直接上代码! const Http = require('http'); const url = require('url

之前用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');

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: 详解用node.js实现简单的反向代理

本文链接: https://www.lsjlt.com/news/12542.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作