【问题标题】:Next.js API | fetch data on Production Server | URL-pathNext.js API |在生产服务器上获取数据 | URL 路径
【发布时间】:2021-05-30 21:42:52
【问题描述】:

谁能解释一下如何在生产服务器中使用下一个 API? 在我的本地工作一切正常,但在部署到 vercel 时,出现此错误:

FetchError: request to http://localhost:3000/api/notes failed, reason: connect ECONNREFUSED 127.0.0.1:3000

我没有完全访问他们的服务器和端口是有道理的...但我在我的应用程序中尝试了仅const res = await fetch('/api/notes'); 但得到了TypeError: Only absolute URLs are supported

所以我应该为此使用我的静态 DNS 吗?但是从本地主机提取数据会更容易,更快,而不是上网然后在服务器上返回数据后......不要得到它

【问题讨论】:

标签: api next.js production-environment vercel dev-to-production


【解决方案1】:

您需要添加您加入的服务器的路径才能部署。示例:

const res = await fetch('https://mynextapp.vercel.app/api/notes');

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 2013-05-01
  • 2019-09-06
  • 2022-06-23
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2020-07-03
相关资源
最近更新 更多