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

引入这个

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': '',},
        }),
    )
}

你可能感兴趣的:(React学习笔记,react.js,javascript,前端)