Next has a built in API route https://nextjs.org/docs/api-routes/introduction
It uses /pages/api
Is it possible to change the default path from /api/* to something else like /myApi/*?
I was thinking about adding it to exportPathMap https://nextjs.org/docs/api-reference/next.config.js/exportPathMap
Any suggestions?
I believe you can't change /api
path because Next.js looks specifically in that location
// Regex for API routes
export const API_ROUTE = /^\/api(?:\/|$)/
If you want to make /api
directory work as any other directory in /pages
you can use rewrite
option.
next.config.js
module.exports = {
rewrites: [
{ source: '/api/:path*', destination: '/another-directory/:path*' }
],
};
In that case requesting /api
would serve content of /another-directory
.
However, you can write a custom server for API routes. Note, that you might need to disable or overwrite default file system routing.
Suggested reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With