在Next.js 13中如何在动态路由中使用路由处理程序?

huangapple go评论60阅读模式
英文:

How do I use route handler inside a dynamic route in nextjs 13?

问题

以下是翻译好的部分:

这是我的文件结构:

当我在本地运行端口3000时,当我访问localhost:3000/api/create时,位于/app/api/create/route.js的路由处理它并返回正确的响应。但是,如果我需要访问localhost:3000/dashboard/api/create,它会返回错误,因为动态路由正在处理它,而API路由没有被命中。我的API只返回一个对象{"hello":"world"}作为响应,我的主页和动态页面也很简单。我只想从任何动态路由中访问/api/create API路由。任何帮助都将不胜感激,谢谢。

已尝试的解决方案:
我尝试过复制API文件夹并将其移动到[...slug]文件夹中,但没有成功。

英文:

This is my file structure:

在Next.js 13中如何在动态路由中使用路由处理程序?

Say I have it running locally on port 3000, when I hit localhost:3000/api/create, the route in /app/api/create/route.js handles it and returns the correct response.
but say I have to hit localhost:3000/dashboard/api/create, it returns an error since the dynamic route is handling it and the api route isn't being hit. My API is only returning an object {"hello":"world"} as response and my home page & dynamic pages are also simple.
All I want is to hit the /api/create api route from any dynamic route.
Any help would be appreciated, thanks.

Solutions Tried:
I have tried duplicating the api folder and moving it inside [...slug] folder but to no avail.

答案1

得分: 0

对于localhost:3000/dashboard/api/create生效,你需要如下的目录结构:

app
├─ [slug]
|    ├─ api
|    |   └─ create
|    |        └─ route.js
|    └─ page.js
...

[...slug] 是一个捕获所有片段的部分。你可以使用 [slug][[...slug]] 这个可选的捕获所有片段。

另外一点:
根据他们的文档,似乎在路由中不包含单词 "create",所以向 /dashboard/api 发送一个 POST 请求会隐含着创建操作。这会使你的目录结构更加简洁,因为你可以省略 "create" 目录:

app
├─ [slug]
|    ├─ api
|    |   └─ route.js
|    └─ page.js
...
英文:

For localhost:3000/dashboard/api/create to work, you would need a dir structure as follows:

app
├─ [slug]
|    ├─ api
|    |   └─ create
|    |        └─ route.js
|    └─ page.js
...

[...slug] is a catch-all segment. You would need to use either [slug] or [[...slug]] the optional catch-all segment.

On another note:
From their docs, they don't seem to include the words "create" in the route so sending a POST to /dashboard/api implies a create. This would make your dir structure a bit more concise since you can drop the "create" dir:

app
├─ [slug]
|    ├─ api
|    |   └─ route.js
|    └─ page.js
...

答案2

得分: 0

从捕获所有动态路由内部触发API路由不可行/不支持,因为捕获所有需要获取URL的完整路径。
因此,我将API路由保留在我的/src文件夹之外,如果我需要从动态路由内的页面中使用fetch调用它,我会调用在我的/src文件夹中指定的API路由,而不需要在动态路由内创建API路由。

英文:

Hitting an api route from inside a catch-all dynamic route isn't possible/supported, since catch-all needs gets the complete path of the url.
So I kept my api route outside in my /src folder, and if I have to call it from a page inside my dynamic route using fetch, I call the api route that is specified in my /src folder itself, and I do not need to create an api route inside my dynamic route at all.

huangapple
  • 本文由 发表于 2023年5月21日 23:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定