【前端技术】基于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);
原创文章 54 获赞 168 访问量 63万+

猜你喜欢

转载自blog.csdn.net/bojie5744/article/details/76070192