React 应用程序,Node.js 服务器图像未渲染

问题描述 投票:0回答:1

This is my Server.jsThis is my Homescreen.jsx

After running the server this error is flashing here 我在这里正确编写了路径“/products”,但在运行服务器后错误仍在闪烁,并且产品图像未呈现

node.js react-hooks axios mern
1个回答
0
投票

提示:StackOverflow 最佳实践建议将代码粘贴(并格式化)到您的问题上,而不是代码打印屏幕上。但代码足够清晰,让我可以回答你的问题:

您似乎正在从“./Data/products”文件模拟数据库。问题是,您无法将任何目录中的任何文件提供给 Express 应用程序。您必须将 Express 设置为使用“静态”或“公共”目录中的文件。检查这里: https://expressjs.com/en/starter/static-files.html

简单的测试方法 - 尝试:

app.get("/products", (req, res) => {
  res.json({id: 1, name: "foo", description: "bar"})
}

PS:首页还不需要;为了避免必须调试可能的前端错误以及您已知的后端错误,请测试与前端分离的“/products”的响应(使用 Postman 或 Insomnia)。这样,您就可以过滤更多错误可以隐藏的地方!

© www.soinside.com 2019 - 2024. All rights reserved.