反应 - 自添加代理以来公开返回 404 的图像 [英] react - Images in public return 404 since added proxy

查看:54
本文介绍了反应 - 自添加代理以来公开返回 404 的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的图像设置在 public/avatars 中并通过 <img src='avatars/my-image1.svg'/>

I set my images inside the public/avatars and render them via <img src='avatars/my-image1.svg' />

我的应用程序运行于:http://localhost:3001

它工作正常,但因为我在 package.json 中添加了一个代理:

it works fine, but since I added a proxy inside the package.json:

代理:http://localhost:3000

图像返回 404(未找到)

the images return 404 (not found)

有人知道如何修复它吗?

Does anyone have any idea how to fix it?

推荐答案

解决方案是通过 src/setupProxy.js(而不是通过 package.json)为端点设置代理

The solution was to set the proxy for the endpoint via src/setupProxy.js (not via package.json)

并将以下内容放入其中:

and place the following contents in it:

const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
  app.use(
    '/your_api',
    createProxyMiddleware({
      target: 'http://localhost:3000',
      changeOrigin: true,
    })
  );
};

链接:https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually

完整说明图片

这篇关于反应 - 自添加代理以来公开返回 404 的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆