【发布时间】:2021-11-18 16:39:44
【问题描述】:
在我的Next.js 项目中,我使用next-connect 包创建了类似route->middleware->endpoint 的表达模式。
我有这个路线模式:
/api/tours
/api/tours/:id
/api/tours/top-5-cheap
/api/tours/stats
/api/tours/monthly-plan
...
在我的pages/api/tours/index.js 文件中,我添加了一个路由来捕获 api/tours 和所有其他子路由,例如 api/tours/top-5-cheap。
根据文档,这应该有效。但是只有 api/tours 可以正常工作,对 api/tours/subroute 的任何请求都会给出page not found error.Docs: next-connect
import nc from 'next-connect'
const mainRoute = nc({ attachParams: true })
const subRoute1 = nc().use(mid1).get((req, res) => { res.end("api/tours/top-5-cheap") });
const subRoute2 = nc().use(mid2).use(mid3).post(endpoint2);
const subRoute3 = nc().use(mid4).use(mid5).use(mid6).get((req, res) => { res.end("api/tours/monthly-plan") })
mainRoute
.use("/top-5-cheap", subRoute1)
.use("/stats", subRoute2)
.use("/monthly-plan", subRoute3)
.get((req, res) => { res.end("api/tours") })
export default mainRoute
我希望能够从pages/api/index.js 文件中捕获对 api/tours 和 api/tours/subroute 的所有请求,而不是为每个子路由创建一个文件
欢迎任何建议或帮助
【问题讨论】:
标签: reactjs next.js next-router next-connect