express實現反向代理

var http = require('http');
var httpProxy = require('http-proxy');
var PORT = 1234;
//創建代理服務器監聽捕獲異常事件
var proxy = httpProxy.createProxyServer();
proxy.on('error',function(err,req,res){
res.end();//異常事件不處理
});
var app = http.createServer(function(req,res){
//執行反向代理
proxy.web(req,res,{
//目標地址
target:'https://www.baidu.com',
changeOrigin: true,  
});
});
app.listen(PORT ,'192.168.200.84',function(){
console.log('server is runing at %d',PORT);
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章