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

44 天前
 seekafter

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

copyByUrl 方法
[nuxt] [request error] [unhandled] [500] [GET] "/yd/aaa": <no response> 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:\dev\wsProjects\xxdl-yingdao-tool\server\routes\yd\copyByUrl.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/')

目前的解决办法是 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 在遇到这种需要互相调用的接口下,是该怎么写接口呢? 闲鱼问了三个人都没给解决...说没遇到过

884 次点击
所在节点    Node.js
10 条回复
airyland
44 天前
试试写成工具函数来调用
seekafter
44 天前
解决了,webstorm 输入$fetch 会自动导包,把他的 import 删除就好了....
zuck3131
44 天前
问一下 如果用 useFetch 换掉这个$fetch 可以用吗
seekafter
43 天前
好像不行, useFetch 是用在.vue 页面中的
crocoBaby
43 天前
nuxt3 能不能直接作为 webapi 服务器?类似 express 那样
seekafter
43 天前
@crocoBaby 可以的,nuxt 是个前后端框架,能单独用也能一块用
crocoBaby
43 天前
@seekafter 也是用 express 吗?还是说框架内置了 router 和 control?
seekafter
42 天前
crocoBaby
42 天前
@seekafter 看样子是不需要 express 了,我学习一下
seekafter
42 天前
@crocoBaby 加油,我也是刚开始学,在做项目

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1027253

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX