Reactjs 在react js中刷新页面时获取错误为“404未找到”

Reactjs 在react js中刷新页面时获取错误为“404未找到”,reactjs,Reactjs,当在react js中刷新页面时,我发现错误404未找到。它在本地主机上工作,但当我在服务器上移动代码时,出现了此错误。我也遇到了此问题,并且与服务器配置有关。您需要做的是配置服务器,以便在访问/*时返回index.html 现在您可能只有/配置了返回index.html 如何做到这一点,取决于您的技术: 快车 app.get'/*',functionreq,res{ res.sendFilepath.join\u dirname,“path/to/your/index.html”,funct

当在react js中刷新页面时,我发现错误404未找到。它在本地主机上工作,但当我在服务器上移动代码时,出现了此错误。

我也遇到了此问题,并且与服务器配置有关。您需要做的是配置服务器,以便在访问/*时返回index.html

现在您可能只有/配置了返回index.html

如何做到这一点,取决于您的技术:

快车

app.get'/*',functionreq,res{ res.sendFilepath.join\u dirname,“path/to/your/index.html”,functionerr{ 如果出错{ res.status500.senderr } }
} 您必须以这样的方式配置服务器:如果找不到请求的资源,则服务器应始终返回index.html文件

您可以使用以下代码:

if(process.env.NODE_ENV==='production'){
    app.use(express.static('client/build'));
    const path=require('path');
    app.get('*',(req,res)=>{
    res.sendfile(path.resolve(__dirname,'client','build','index.html'));
  })
}

请阅读并阅读错误日志文件,它将告诉您该怎么做。