【前端技術】基於nodejs開發的web工程開啓代理轉發功能

背景:
web開發中,我們需要訪問mock server則需要把web中所有請求代理到mockserver中。

在啓動web開發模式的腳本中,添加以下代碼。其中a-api是一個訪問路徑。

const proxy = require('http-proxy-middleware');//引入代理中間件
const aProxy = proxy('/a-api', { target: 'http://127.0.0.1:7001',changeOrigin: true });

const bProxy = proxy('/b-api', { target: 'http://127.0.0.1:7001',changeOrigin: true });

 
var options = {
        target: 'http://127.0.0.1:7001',  
        changeOrigin: true,    
        pathRewrite: {
            '^/server-api/app' : '/app'            
        }
    };
這裏是把/server-api/app/* 請求 轉發到http://127.0.0.1:7001/app
const apiProxy = proxy(options);
app.use('/server-api/app/*',apiProxy);


app.use('/a-api/*',apiProxy);
app.use('/b-api/*',serverApiProxy);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章