NextJS 服务器端获取

编程入门 行业动态 更新时间:2024-10-06 18:26:22

NextJS <a href=https://www.elefans.com/category/jswz/34/1771251.html style=服务器端获取"/>

NextJS 服务器端获取

我正在尝试从 NextJS(开发模式)的 API 端点中的另一个资源获取数据并收到以下错误:

error - TypeError: Failed to parse URL from httр://localhost:3000/api/test
    at Object.fetch (node:internal/deps/undici/undici:14152:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handler (webpack-internal:///(api)/./src/pages/api/checkout.js:8:20)
    at async Object.apiResolver (XXX\node_modules\next\dist\server\api-utils\node.js:372:9)
    at async DevServer.runApi (XXX\node_modules\next\dist\server\next-server.js:514:9)
    ... 5 lines matching cause stack trace ...
    at async XXX\node_modules\next\dist\server\base-server.js:157:99 {
  page: '/api/checkout',
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:399:5)
      at URL.onParseError (node:internal/url:565:9)
      at new URL (node:internal/url:645:5)
      at new Request (node:internal/deps/undici/undici:6947:25)
      at fetch2 (node:internal/deps/undici/undici:13315:25)
      at Object.fetch (node:internal/deps/undici/undici:14150:18)
      at fetch (node:internal/process/pre_execution:241:25)
      at infoAPI (webpack-internal:///(api)/./src/modules/reporting/api/infoAPI.js:17:12)
      at handler (webpack-internal:///(api)/./src/pages/api/checkout.js:8:100)
      at Object.apiResolver (XXX\node_modules\next\dist\server\api-utils\node.js:372:15)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async DevServer.runApi (XXX\node_modules\next\dist\server\next-server.js:514:9)
      at async Object.fn (XXX\node_modules\next\dist\server\next-server.js:828:35)
      at async Router.execute (XXX\node_modules\next\dist\server\router.js:243:32)
      at async DevServer.runImpl (XXX\node_modules\next\dist\server\base-server.js:432:29)
      at async DevServer.run (XXX\node_modules\next\dist\server\dev\next-dev-server.js:831:20)
      at async DevServer.handleRequestImpl (XXX\node_modules\next\dist\server\base-server.js:375:20)
      at async XXX\node_modules\next\dist\server\base-server.js:157:99 {
    input: 'httр://localhost:3000/api/test',
    code: 'ERR_INVALID_URL'
  }
}

这是我在

/src/pages/api/checkout.js
中使用的代码:

async function infoAPI() {
  return fetch('httр://localhost:3000/api/test');
}

export default async function handler(req, res) {
  const result = await infoAPI();

  //...

  res.status(200).json({ message: 'success', data: result });
}

httр://localhost:3000/api/test
只是一个例子。无论
fetch
中提供什么URL - localhost或external,错误都是一样的。我需要像这样在我的应用程序中调用一些外部 URL。

它有什么问题?

更新:

http.get('httр://localhost:3000/api/test', (res) => {/*...*/})
请求相同的问题:

error - TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:399:5)
    at URL.onParseError (node:internal/url:565:9)
    at new URL (node:internal/url:645:5)
    at new ClientRequest (node:_http_client:134:30)
    at request (node:http:97:10)
    at Object.get (node:http:108:15)
    at result (webpack-internal:///(api)/./src/pages/api/checkout.js:15:53)
    ... 
{
  input: 'httр://localhost:3000/api/test',
  code: 'ERR_INVALID_URL',
  page: '/api/checkout'
}

我在 Node v19.4.0. 上运行 Next v13.2.4

回答如下:

更多推荐

NextJS 服务器端获取

本文发布于:2024-05-30 15:52:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器端   NextJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!