公司在做銀行項目,開發都在雲桌面。不能直接訪問服務器上的接口服務,但是可以連接到雲桌面,所以搭建個代理之後就可以愉快的在本地開發啦
安裝http-proxy
模塊
>npm install http-proxy --save-dev
新建js文件
// server.js
const http = require('http');
const httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
proxy.on('error', function(err, req, res){
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Something went wrong. And we are reporting a custom error message');
})
var server = http.createServer(function(req, res) {
console.log('connect...');
proxy.web(req, res, {target: 'http://10.30.30.210:8881'});
})
console.log('listening on port 9999');
server.listen(9999);
運行js
>node server.js
啓動服務後,前端開發的代理地址填寫http://localhost:9999
,訪問時會代理請求到http://10.30.30.210:8881
http-proxy
的功能還有不少,因爲暫時只用到這些,所以沒有再深入研究,等哪天用到的時候再整理整理吧
參考: