Next 有一个内置的 API 路由
https://nextjs.org/docs/api-routes/introduction
它使用/pages/ api
是否可以从/ 更改默认路径? api /* 到其他类似/ myApi /*?
我正在考虑将它添加到 exportPathMap
https://nextjs.org/docs/api-reference/next.config.js/exportPathMap
有什么建议?
最佳答案
我相信你改变不了/api
路径,因为 Next.js 专门在该位置查找
// Regex for API routes
export const API_ROUTE = /^\/api(?:\/|$)/
如果您想制作
/api
目录与 /pages
中的任何其他目录一样工作您可以使用 rewrite
选项。next.config.js
module.exports = {
rewrites: [
{ source: '/api/:path*', destination: '/another-directory/:path*' }
],
};
在这种情况下,请求
/api
将提供 /another-directory
的内容.但是,您可以为 API 路由编写自定义服务器。请注意,您可能需要禁用或覆盖默认文件系统路由。
推荐阅读:
关于next.js - Nextjs 将/pages/api 更改为/pages/myApi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60874250/