You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow Next.js to support custom directory paths for API routes. For example, an option to move API routes to a server directory.
Non-Goals
Background
I have been working with Next.js for a while, and I find that the current convention of placing API routes in the pages/api or app/api folder is fine for smaller projects. However, as the project grows, I would like to separate the API routes into a custom server directory to keep the project structure clean and maintainable.
Currently, in large-scale applications, it becomes a challenge to differentiate between client-side and server-side code when both are located in pages/api or app/api.
app/api is not necessary. In App Router, it is the file name that makes a file a router handler, route.ts, not its folder location. So, for App Router, your issue is already solved. It is not mandatory there.
In Pages Router, pages/api, was done because, it is a way to separate server logic, and keep a structure clean and maintainable, since in Pages Router, all files within pages are reachable by clients. So, to make it obvious which ones would serve React made pages, or be API's for your backend, the API Routes, were placed under pages/api. Note that, initially, these were supposed to be part of the backend for your frontend BFF, model.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Allow Next.js to support custom directory paths for API routes. For example, an option to move API routes to a server directory.
Non-Goals
Background
I have been working with Next.js for a while, and I find that the current convention of placing API routes in the pages/api or app/api folder is fine for smaller projects. However, as the project grows, I would like to separate the API routes into a custom server directory to keep the project structure clean and maintainable.
Currently, in large-scale applications, it becomes a challenge to differentiate between client-side and server-side code when both are located in pages/api or app/api.
Proposal
Looking forward to hearing your thoughts on this.
Beta Was this translation helpful? Give feedback.
All reactions