node 代理

const express = require('express');
const proxy = require("http-proxy-middleware");
const app = express();
const path = require("path")
 
var http = require('http');  
   
var qs = require('querystring');  
 
// User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
const request = require("request").defaults({
  proxy: "http://proxy.tplife.com:31151",
  rejectUnauthorized: false,
})
 
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());
//设置允许跨域访问该服务.
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1')
  console.log("1")
  if (req.method == "OPTIONS") res.send("");
  else  next();
});
// statusCode
app.post('/api/**', function (req, res) { 
   
  request({
    url: "http://10.100.129.80:8088" + req.originalUrl.replace("/api", ""),//请求路径
    method: "POST",//请求方式,默认为get
    headers: {//设置请求头
      "content-type": "application/json",
    }, 
    // form: req.body
    body: JSON.stringify(req.body) //post参数字符串
  }, function (error, response, body) {
    if (response) {
      console.log(response)
      res.send(response && response.body);
    } else { 
      res.send(JSON.stringify({}));
    }
    // if (!error && response.statusCode == 200) {
    // }
  });
});
app.get('/api/**', function (req, res) {
  request({
    url: "http://10.100.129.80:8088" + req.originalUrl.replace("/api", ""),//请求路径
    method: "GET",//请求方式,默认为get
    headers: {//设置请求头
      "content-type": "application/json",
    },
  }, function (error, response, body) {
    res.send(response && response.body);
    // if (!error && response.statusCode == 200) {
    // }
  });
});
 
// app.use('/api/**',
//   function (req, res) {
//     console.log(req)
//     console.log(req.body)
//     console.log(req.query)
//     res.send('itemPage');
//   }
//   //   proxy((pathname, req) => {
//   //     console.log(pathname)
//   //     // console.log(req)
//   //     return true;
//   //   }, {
//   //       // 代理目标地址
//   //       target: "http://10.100.129.80:8088/", // 后台接口域名 
//   //       ws: true,        //如果要代理 websockets,配置这个参数
//   //       secure: false,  // 如果是https接口,需要配置这个参数
//   //       changeOrigin: true,  //是否跨域 
//   //       onProxyReq: function (proxyReq, req, res, options) {
//   //         // console.log(proxyReq)
//   //         // console.log(res)
//   //       //   console.log(req.body)
//   //       //   if (req.body) {
//   //       //     let bodyData = JSON.stringify(req.body);
//   //       //     // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
//   //       //     // proxyReq.setHeader('Content-Type', 'application/json');
//   //       //     // proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
//   //       //     // stream the content
//   //       //     proxyReq.write(bodyData);
//   //       //   }
//   //       },
//   //       pathRewrite: {
//   //         // 地址重写
//   //         "/api": ""
//   //       }
//   //     })
//   // .on('proxyReq', function onProxyReq(proxyReq, req, res) {
//   //   proxyReq.setHeader('x-added', 'foobar');
//   // })
// );
// app.use('/taiping-attendance-management/**',
//   proxy((pathname, req) => {
//     console.log(pathname)
//     // console.log(req)
//    return true;
//   },{
//     // 代理目标地址
//     target: 'http://127.0.0.1:8082',
//     changeOrigin: true,
//     // pathRewrite: {
//     // 地址重写
//     // "/ibot": "/ibot"
//     // }
//   }));
//   '/taiping-attendance-management': {
//      target: 'http://127.0.0.1:8082',
//     //target:'http://10.28.151.13:8082',
//     changeOrigin: true,
//     pathRewrite: {
//       // '^/taiping-attendance-management': ''
//     }
//   }
 
// app.use("/",
//   proxy({
//     // 这里是vue/react启动后需要访问网页的地址
//     // **修改了这里
//     target: "http://localhost:8080",
//     // target: "http://zyadmin.xiaoluge.top",
//     // target: "http://106.12.76.26:7001",
//     changeOrigin: true,
//     ws: true,        //如果要代理 websockets,配置这个参数
//     secure: false,  // 如果是https接口,需要配置这个参数
//     changeOrigin: true,  //是否跨域 
//   }))
 
 
// app.use(express.static(path.join(__dirname, 'assets')))
app.listen(8288, () => {
  console.log("项目启动与:http://localhost:8288");
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值