react中代理异常 proxy is not a function

使用cnpm install create-react-app 构建react项目,进行跨域相应的配置之后,出现了proxy is not a function 的报错

原本的配置:

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

// module.exports = function (app){
//     app.use('/api',
//     proxy({
//         target:'http://localhost:3300',
//         changeOrigin:true,
//     }))
// }

启动报错信息:

proxy is not a function
error Command failed with exit code 1.

原因: npm 的http-proxy-middleware ,发现1.x做了很多的改变

正确的写法:

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

module.exports = function (app){
    app.use('/api',
    createProxyMiddleware({
        target:'http://localhost:3300',
        changeOrigin:true,
    }))
}

版权声明:本文为weixin_46042888原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。