react + webpack 代理的两种方式

2023-04-13  本文已影响0人  Petricor

前言

1.0 通过 setupProxy 解决

const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
    app.use(
        '/api',
        createProxyMiddleware({
            target: 'http://localhost:5000',
            changeOrigin: true,
            pathRewrite: {
                "^/api": "/api"
            },
        })
    );
};

2.0 通过 craco 解决跨域问题

//craco.config.js 
const path = require('path')
module.exports = {
    //配置代理解决跨域
    devServer: {
        port: 3009,
        proxy: {
            '/api': {
                target: 'http://114.215.183.171:5002',
                changeOrigin: true,
                pathRewrite: {
                    "^/api": "/api"
                }
            },
       }
}
上一篇 下一篇

猜你喜欢

热点阅读