http-proxy-middleware代理转发到不同的IP和端口

本文档详细介绍了如何使用http-proxy-middleware进行代理服务器配置,适用于开发过程中接口文档中存在不同IP和端口号的情况。通过配置option下的router属性,可以实现路径到特定IP和端口的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http-proxy-middleware一个轻松的配置代理服务器的中间件

开发中若接口文档中接口有不同的IP和端口号,需要在option下的router属性中配置path:转向的端口和IP,我这里使用的是path来匹配请求。参考文档:http-proxy-middleware

/**
 * @file config for proxy, useful only in dev.
 *
 * @author taohang(taohang@baidu.com)
 *
 * The dev server is using http-proxy-middleware for proxying,
 * so you can refer to its docs for detailed usage.
 * see https://github.com/chimurai/http-proxy-middleware for documentation.
 */

module.exports = {
    proxy: {
        // proxy all requests starting with /api to target,
        // use /*/* will proxy all requests starting with any string
        // proxy useful only while path exist && option.target is exist
        path: ['/eam/asset/', '/currentUser', '/eam/host/', '/eam/', '/platform/api/v1', '/apptree/v1',
            '/apptreeNew', '/asset',
            '/eam_task', '/taskNames', '/intervals', '/status', '/tasks', '/columns', '/network/subnet/physical'],
        option: {
            // the target to request
            // target: 'http://10.130.71.24:8050',		// 上线地址
            // target: 'http://10.132.144.17:8009',   	// 测试地址
            // target: 'http://10.132.144.17:8050',  	// 物理机资产地址
            target: 'http://10.132.144.31:8010',  		// 资产测试地址
            // changes the origin of the host header to the target URL
            changeOrigin: true,
            // object, adds request headers. (Example: {Cookie:'mycookie'})
            headers: {
                // get cookie by visit the target url
                Cookie: ''
            },
            // 这里表示不同的path对应不同的转发 path: url
            router: {
                '/network/subnet/physical': 'http://10.132.144.31:8009'
            }
        }
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值