页面重新加载或粘贴URL提供404,并带有Mern堆栈

发布于 2025-02-12 17:10:20 字数 843 浏览 1 评论 0原文

我正在建立一个简单的网站,该网站使用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"));
      });
    
    }

我进行了一些效果

  1. 即使我删除了 带有名称为“注册”的图像
  2. 如果我去,则 到我的网站并单击注册,
  3. 如果我访问域/注册,它正在显示图像,因此即使上面的代码段也将其视为静态文件的请求,并且不让它传递并解析为索引.html

关于apache的服务器

  1. 与apache on /api on /api
  2. 它会破坏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.

  1. I added an image with name 'register'
  2. If I go to my website and click on register it works
  3. 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

  1. Ubuntu with Apache with proxy on /api to node server
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南烟 2025-02-19 17:10:20

尝试此代码对我有用。并参考 this this for path.stey.stecome strone.leslove.lesolve.strove.lesolve.stecl.stecl.stover.incloves。和路径

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

Trying this code worked for me. And refer this for the difference between path.resolve and path.join

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文