首页 > 解决方案 > 加载资源失败:服务器响应状态为 404(未找到) ENOENT:没有这样的文件或目录,stat '/client/build/index.html'

问题描述

https://github.com/sw9142/thankshare

在过去的几天里,我一直在努力解决这个错误,但我不能..

检查heroku日志,它说找不到'/client/build/index.html'。

2021-07-02T04:46:14.791646+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/client/build/index.html'

这是开发工具中出现的错误和另一个错误(这是导致404错误的原因吗??)

Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "font-src 'self' data:". 


Failed to load resource: the server responded with a status of 404 (Not Found)createError.js:16 
Uncaught (in promise) Error: Request failed with status code 404
at e.exports (createError.js:16)
at e.exports (settle.js:17)
at XMLHttpRequest.p.onreadystatechange (xhr.js:62)

所以这就是我一直在尝试解决这个问题的方法,将以下内容添加到 server.js

if (process.env.NODE_ENV === "production") { 
        app.use(express.static(path.join(__dirname, "/client/build")));  
        app.get('*', function (req, res) {
        const fullPath = path.join(__dirname,  '../client', 
                                  'build', 'index.html')
         res.sendFile(fullPath)
   })
 }

谁知道我做错了什么,请告诉我!!!

标签: node.jsherokuhttp-status-code-404

解决方案


推荐阅读