nuxt3 后端(server)目录接口调用问题求解答~

14次阅读

共计 1130 个字符,预计需要花费 3 分钟才能阅读完成。

server 里面写了两个接口, 调用地址分别是 /yd/copyByUrl 和 /yd/aaa
用 postman 都能调用成功, 但是在 /yd/copyByUrl 里面调用 /yd/aaa 时报错:

copyByUrl 方法
[nuxt] [request error] [unhandled] [500] [GET] "/yd/aaa":  Failed to parse URL from /yd/aaa
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  at async $fetch2 (/E:/dev/wsProjects/xxdl-yingdao-tool/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:261:15)
  at Object.handler (E:devwsProjectsxxdl-yingdao-toolserverroutesydcopyByUrl.post.ts:30:18)
  at async Object.handler (/E:/dev/wsProjects/xxdl-yingdao-tool/node_modules/h3/dist/index.mjs:1962:19)
  at async Server.toNodeHandle (/E:/dev/wsProjects/xxdl-yingdao-tool/node_modules/h3/dist/index.mjs:2249:7)
[Vue Router warn]: No match found for location with path "/yd/copyByUrl"
[Vue Router warn]: No match found for location with path "/yd/copyByUrl"
[Vue Router warn]: No match found for location with path "/yd/copyByUrl"

调用的代码:
let res = await $fetch(‘/yd/aaa/’)

nuxt3 后端 (server) 目录接口调用问题求解答~

目前的解决办法是 gpt 给的:

const req = event.req;
  const protocol = req.headers['x-forwarded-proto'] || 'http';
  const host = req.headers.host;
  const baseUrl = `${protocol}://${host}`;

  console.log("Base URL:", baseUrl);

通过拼接 baseUrl 和路径来实现调用的

请问,nuxt3 在遇到这种需要互相调用的接口下, 是该怎么写接口呢?
闲鱼问了三个人都没给解决 … 说没遇到过

正文完
 0