简单的html js node 前端直接使用反向代理软件

先放上已经打包好的地址

https://gitee.com/Amengxiaoya/node-proxy.git 切记proxyConfig.json 设置代理ip为自己的ipv4地址 (cmd+ipconfig)

再放上源码地址

https://gitee.com/Amengxiaoya/node-re-quest.git 有点小乱,我测试用的(该项目只用到一个proxy.js文件)。

下载后第一步 cnpm install 然后node的pkg库的打包命令 pkg -t node14-win proxy.js

主要的proxy.js文件源码

`//代理
const querystring = require('querystring');
const util = require('util');
const http = require('http');
const request = require('request');
const formidable = require("formidable");
const path = require("path");
const fs = require("fs");
const FormData = require('form-data')

const {
hostIp,
apiPort,
redirUrl,
customQuery,
consoleConfig:{apiSuc,apiFail,apiMsg,removeFile,removeMsg}
} = JSON.parse(fs.readFileSync(process.cwd()+'/proxyConfig.json','utf-8'));

function saveFiles(req,options,callback){
const promiseCase=[];
var form = new formidable.IncomingForm();
form.keepExtensions = true;
form.multiples = true;
form.uploadDir = path.join(process.cwd());
form.parse(req, function(err, fields, files) {
//const formData = new FormData()
options.formData = {};
for (let key in fields) {
//formData.append(key, fields[key])
options.formData[key]=fields[key]
}
for (let key in files) {
let newFilepath='Tmp-'+files[key].originalFilename;
fs.renameSync(files[key].newFilename,newFilepath)
//formData.append(key, fs.createReadStream('./'+newFilepath))
options.formData[key]=fs.createReadStream('./'+newFilepath,{highWaterMark:9999999})
new Promise((rev)=>{
promiseCase.push(rev)
}).then(()=>{
fs.unlink('./'+newFilepath,function(err){
if(removeFile="0"){
console.log(err?'清理临时文件异常':'删除临时图片成功');
}
if(removeMsg
="0"&&err){
console.log(err);
}
})
})
}
request(options, function(error, response, body){
callback(error, response, body)
for (let item of promiseCase) {
item()
}
promiseCase.splice(0,)
})
//pipe(fs.createWriteStream(files.file.originalFilename))//formData.pipe()
})
}

//创建代理服务
const httpServer = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', '
');
if (req.method == 'OPTIONS') {
// 回复OPTIONS
res.end('');
return
}
/* 正式代理 */
let options = {
method: req.method,
header:req.headers,
encoding:null
};
if(customQuery!'-1'){
options.headers={};
for (let key in customQuery) {
options.headers[key]=req.headers[customQuery[key]]
}
}
options.url = redirUrl + req.url
function callback(error, response, body) {
res.setHeader('Content-Type', response.headers['content-type']);
res.statusCode = response.statusCode
if (!error) { //删除 && response.statusCode === 200
if(apiSuc
="0"){
console.log(req.url + '成功代理')
}
res.end(body);
} else {
if(apiFail="0"){
console.log(req.url + '请求异常')
}
if(apiMsg
="0"){
console.log(error)
}
res.end("{'code':-1,'msg':'node请求异常'}");
}
}
if (options.method === 'GET') {
request(options, callback);
}else if (options.method === 'POST') {
if(/^multipart/form-data/.test(req.headers['content-type'])){
saveFiles(req,options,callback)
}else{
// 定义了一个post变量,用于暂存请求体的信息
var post = '';
// 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
req.on('data', function(chunk) {
post += chunk;
});
req.on('end', function() {
options.body=post
request(options, callback);
});
}
}
}).listen(apiPort, hostIp, () => {
console.log('开启代理---' + hostIp + ':' + apiPort + '---为---' + redirUrl,'by#2585266774@qq.com#')
});`

上一篇:移动端堆栈关键行定位的新思路


下一篇:【运维】MariaDB_galera_cluster集群