在 Vercel 上部署 NEXT.js 应用程序时,我应该如何防止 ECONNREFUSED 错误?

时间:2021-04-12 22:19:43

标签: next.js vercel getstaticprops

我正在尝试部署一个个人投资组合应用程序,该应用程序利用内部 API 端点来查询数据库。 在部署过程中,我收到以下错误:FetchError: request to http://localhost:3000/api/projects/all failed, reason: connect ECONNREFUSED 127.0.0.1:3000

该应用程序在本地会话期间运行良好,但经过一些研究,我意识到在 getStaticProps 中使用 fetch 是不正确的。但是,我无法提出解决方案。我应该忘记端点并从 getStaticProps 中查询数据库,还是应该更新 URL 以反映 Vercel 提供的 URL?我一整天都在兜兜转转,希望得到任何提示,谢谢!

// index.js:

export async function getStaticProps() {
  
  const resProjects = await fetch('http://localhost:3000/api/projects/all');
  const projects: Project[] = await resProjects.json();

  const resSkills = await fetch('http://localhost:3000/api/skills/all');
  const skills: Skills[] = await resSkills.json();

  return {
    props: {
      projects,
      skills,
    }
  }
}

//api/projects/all

export default async (req: NextApiRequest, res: NextApiResponse) => {
  const { method } = req;

  if (method === "GET") {
    try {
      const allProjects = await prisma.project.findMany();
      res.status(200).json({ projects: allProjects });
    } catch (e) {
      res.status(500).json({ error: e });
    }
  } else {
    res.setHeader('Allow', ['GET'])
    res.status(405).end(`Method ${method} Not Allowed`)
  }
}

0 个答案:

没有答案