Next.js API 路由全部返回“Interval Server Error”正在生产中,但所有路线都在开发中

发布于 2025-01-12 07:15:05 字数 2187 浏览 0 评论 0原文

每当我在生产环境中访问 Next.js 应用程序中的任何 API 路由时,它都会返回 500“内部服务器错误”,但在开发过程中,所有这些都工作得很好,并显示/返回我期望的结果。

the error

我正在使用 AWS Ec2 实例进行部署。

代码可以在这里找到:https://github.com/123om123/NFT-Marketplace

是我所有的API路线。

输入图片此处描述

[...auth0].js 创建以下路由: /api/auth/login, /api/auth/logout, /api/auth/callback,以及 /api/auth/me

如果我尝试访问如下所示的“find_account”API 路由:

  let findAccount = async function () {
    await fetch("/api/find_account", {
      method: "POST",
      body: JSON.stringify({
        DBUrl: DBUrl,
        user_id: user.sub,
      }),
    })
      .then(async (response) => {
        await response.json().then((result) => {
          accountData = result;
          if (accountData.data.allAccounts.nodes[0].addresses !== null) {
            setAddressList(accountData.data.allAccounts.nodes[0].addresses[0].split(","));
          }
        });
      })
      .catch((err) => {
        return err;
      });
  };

它处理如下请求:

export default function handler(req, res) {
  req.body = JSON.parse(req.body);
  fetch(req.body.DBUrl, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      query: `query MyQuery {
        allAccounts(condition: {userId:"${req.body.user_id}"}) {
          nodes {
            addresses
          }
        }
      }`,
    }),
  })
    .then((response) => {
      response.json().then((response) => {
        res.status(200).send(response);
      });
    })
    .catch((err) => {
      res.status(500).send(err);
    });
}

它工作正常并返回来自开发中的 graphql API 的响应,但是在生产中它显示上述错误。

问题似乎是 API 路由甚至没有创建,因此无法访问。所有 API 路线在几周前都有效,但现在似乎已经停止工作了。

Whenever I go to any API route in my Next.js app in production it returns a 500 "Internal Server Error" but in development, all of them work completely fine and show/return what I expect them to.

another example of the error

the error

I am deploying with an AWS Ec2 instance.

the code is available here: https://github.com/123om123/NFT-Marketplace

These are all my API routes.

enter image description here

The [...auth0].js creates the following routes:
/api/auth/login,
/api/auth/logout,
/api/auth/callback, and
/api/auth/me

If I try to access the "find_account" API route like the following:

  let findAccount = async function () {
    await fetch("/api/find_account", {
      method: "POST",
      body: JSON.stringify({
        DBUrl: DBUrl,
        user_id: user.sub,
      }),
    })
      .then(async (response) => {
        await response.json().then((result) => {
          accountData = result;
          if (accountData.data.allAccounts.nodes[0].addresses !== null) {
            setAddressList(accountData.data.allAccounts.nodes[0].addresses[0].split(","));
          }
        });
      })
      .catch((err) => {
        return err;
      });
  };

which handles requests like the following:

export default function handler(req, res) {
  req.body = JSON.parse(req.body);
  fetch(req.body.DBUrl, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      query: `query MyQuery {
        allAccounts(condition: {userId:"${req.body.user_id}"}) {
          nodes {
            addresses
          }
        }
      }`,
    }),
  })
    .then((response) => {
      response.json().then((response) => {
        res.status(200).send(response);
      });
    })
    .catch((err) => {
      res.status(500).send(err);
    });
}

it works fine and returns the response from the graphql API in development, but in production it shows the above error.

The problem seems to be that the API routes aren't even created and are therefore inaccessible. All the API routes worked a few weeks ago, but now they seem to have stopped working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

别把无礼当个性 2025-01-19 07:15:05

看看这个答案,它帮助我解决了与你类似的错误。就我而言,当 vercel 部署 PR 分支时,我收到了 500 内部错误。当我将 PR 合并到 main 并 vercel 进行全新部署后,我现在收到 504 网关错误。下面的答案帮助我找出了原因。

https://stackoverflow.com/a/68774331/12775824

Check out this answer, it helped me solve a bug similar to yours. In my case when vercel was deploying my PR branch I was getting a 500 Internal Error. Once I merge the PR into main and vercel does a fresh deployment, I was now getting a 504 gateway error. The answer below helped me find out why.

https://stackoverflow.com/a/68774331/12775824

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