页面重新加载或粘贴URL提供404,并带有Mern堆栈
我正在建立一个简单的网站,该网站使用Mern堆栈在Apache上运行。 /api上有一个代理(&反向代理)设置,以路由到IP:端口 /API
在过去几个月中的某个时候,任何页面刷新或粘贴URL会导致404。我无法识别原因。
它正在使用客户端路由(基于且正在工作。它仍在local主机上工作,仅在服务器上断路。
现在,当我将用于在服务器上使用的旧代码时,它以相同的方式破裂。这表明环境的变化是
一个开发人员,我正在添加此代码来解决它,
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
我进行了一些效果
- 即使我删除了 带有名称为“注册”的图像
- 如果我去,则 到我的网站并单击注册,
- 如果我访问域/注册,它正在显示图像,因此即使上面的代码段也将其视为静态文件的请求,并且不让它传递并解析为索引.html
关于apache的服务器
- 与apache on /api on /api
- 它会破坏HTTP或HTTPS。
使用了自我签名的证书,但如果有人看过, 这之前或可以给指针关于在哪里进行调试,我将不胜感激。
I am building a simple website that uses MERN stack running on apache.
There is a proxy (& reverse proxy) settings on /api to route to ip:port/api
At some point in the last couple of months, any page refresh or pasting urls causes a 404. I cannot identify what causes this.
It is using client side routing (based and it was working. It is still working on localhost only breaks on the server.
Now when I put the old code that used to work on the server, it breaks the same way. This suggests that a change in the environment is causing/contributing.
One developer I was working added this piece of code to try to address it.
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
It doesn't seem to have any effect even if I remove the if condition. I did a little test.
- I added an image with name 'register'
- If I go to my website and click on register it works
- If I visit domain/register, it is showing the image, so even with the code snippet above it is treating it as a request for static file and not letting it pass on and resolve to the index.html
About the server
- Ubuntu with Apache with proxy on /api to node server
- Uses a self signed certificate but it breaks on http or https.
This seems to be a common problem. If anyone has seen this before or can give pointers on where to debug this, I would appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此代码对我有用。并参考 this this for path.stey.stecome strone.leslove.lesolve.strove.lesolve.stecl.stecl.stover.incloves。和路径
Trying this code worked for me. And refer this for the difference between path.resolve and path.join