接下来有一个内置API路由https://nextjs.org/docs/api-routes/introduction。
它使用/pages/api
是否可以将默认路径从/api/*更改为/myApi/*
我在考虑把它添加到exportPathMap https://nextjs.org/docs/api-reference/next.config.js/exportPathMap
有什么建议吗?
发布于 2020-03-27 19:28:59
我认为您不能更改/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路由编写自定义服务器。注意,您可能需要禁用或覆盖默认的文件系统路由。
建议阅读:
https://stackoverflow.com/questions/60874250
复制相似问题