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");
})
node 代理
最新推荐文章于 2024-03-08 18:21:16 发布