React创建配置代理文件setupProxy.js,启动项目无法访问

React创建配置代理文件setupProxy.js,启动项目无法访问,第1张

引入这个

require('http-proxy-middleware')

如果是低版本的话,这样写可以

const proxy =require('http-proxy-middleware');

如果是高版本(最新版)需要这么写

const {createProxyMiddleware: proxy} =require('http-proxy-middleware');

setupProxy.js文件内容

const {createProxyMiddleware: proxy} =require('http-proxy-middleware');

// 暴露一个module出去
module.exports = function (app){
    app.use(
        proxy('/api0',{
           target:'http://localhost:8081',
           changeOrigin:true,
            pathRewrite: {'^/api0': '',},
        }),
        proxy('/api1',{
            target:'http://localhost:8082',
            changeOrigin:true,
            pathRewrite: {'^/api1': '',},
        }),
    )
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1321562.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-12
下一篇2022-06-12

发表评论

登录后才能评论

评论列表(0条)

    保存