有没有办法在生产环境中禁用所有next.js api路由



在开发阶段,API路由对我来说很适合Mock数据。

在生产环境中,我有自己的后端服务。

有没有办法禁用所有API路由?

感谢@julialves的建议,我发现它可以在next.config.js中添加重定向规则来禁用(重定向到404)所有/api/*请求

const nextConfig = {
async redirects() {
if (process.env.NODE_ENV === "production") {
return [
{
source: "/api/:slug*",
destination: '/404',
permanent: true,
}
];
} else {
return []
}
},
...
}

最新更新