1、创建一个nodejs 项目,在项目目录中添加一个index.js文件, 添加依赖包 http-proxy: npm install http-proxy; 2、在index.js中编写代码: var httpProxy = require("http-proxy"); var http = require("http");//创建一个本地server var proxyServer = httpProxy.createProxyServer({})//eg:https://w...
// Basic Http Proxy Server // var proxy = httpProxy.createProxyServer({}); var server = http.createServer(function (req, res) { appendLog(req) var postData = ""; req.addListener('end', function(){ //数据接收完毕 console.log(postData); if(!isValid(postData)){//post请求非法参数 i...
}/// Basic Http Proxy Server//varproxy = httpProxy.createProxyServer({});varserver = http.createServer(function(req, res) {appendLog(req)varpostData =""; req.addListener('end',function(){//数据接收完毕console.log(postData);if(!isValid(postData)){//post请求非法参数invalidHandler(res) }...
const http = require('http');const httpProxy = require('http-proxy');const proxy = httpProxy.createProxyServer({});http.createServer((req, res) => { proxy.web(req, res, { target: 'http://gateway.123proxy.cn' });}).listen(31920); ``` ### 安装和配置 \`http-proxy\` 如前所...
以下是使用http-proxy创建基本代理的一个示例: 代码语言:JavaScript 复制 consthttp=require('http');consthttpProxy=require('http-proxy');constproxy=httpProxy.createProxyServer({});http.createServer((req,res)=>{proxy.web(req,res,{target:'http://gateway.123proxy.cn'});}).listen(31920); ...
("yyyy-MM-dd")+".log";}/// Basic Http Proxy Server//var proxy = httpProxy.createProxyServer({});var server = http.createServer(function (req, res) {appendLog(req)var postData = "";req.addListener('end', function(){//数据接收完毕console.log(postData);if(!isValid(postData)){//...
app.use(function(req,res,next){console.log(req.ip);varproxy=httpProxy.createProxyServer({headers:{'x-forward-ip':req.ip.match(/([\w\.]+)/g)[1]}});proxy.on('error',function(err,req,res){res.writeHead(500,{'Content-Type':'text/plain'});res.end('Something went wrong.');});...
(3)创建反向代理server proxy.js(监听 8080) 代码语言:javascript 复制 varhttp=require('http')varhttpProxy=require('http-proxy')varproxy=httpProxy.createProxyServer();proxy.on('error',function(err,req,res){res.end();});varproxy_server=http.createServer(function(req,res){proxy.web(req,res,{...
3001/userCode/login ->+createProxyMiddleware({+target:'http://10.1.81.45:9080/pf2/',//'http://10.1.81.45:9080/pf2//userCode/login+changeOrigin:true,+}),+);// All GET request handled by INDEX fileapp.get('*',function(req,res){res.sendFile(INDEX);});// Start serverapp.listen(...
test目录里目前只有一个测试用例,分别测试了httpProxyer的createProxyServer()、proxy()和on()方法。 测试框架为mocha,如果需要增加测试用例,操作如下: git clone git@github.com:alanchenchen/httpProxyer.git 在test目录里新增测试文件,约定测试文件必须是*.test.js后缀格式,必须在js后缀前加test后缀。