Next.js Discord

Discord Forum

Help with api routing app/api/{file} doesnt work for some reason

Answered
Appenzeller Sennenhund posted this in #help-forum
Open in Discord
Appenzeller SennenhundOP
I am very new to next js and cant seem to figure this out,
everywhere in the documentation the route for the api is given as pages/api but by default (using npx create-next-app@latest ) an app folder is created.
I want to create an api route on app/api/hello.js but it doesnt seem to work. what am I doing wrong?
[I also tried changing the name of the app folder to pages folder and that results in the app not working anymore]
Answered by Ray
It should be /app/api/hello/route.js
View full answer

15 Replies

Appenzeller SennenhundOP
@Appenzeller Sennenhund Click to see attachment
It should be /app/api/hello/route.js
Answer
Appenzeller SennenhundOP
whats the difference between app and pages, which one is suggested?
And your code only work on the page router version of api route
Appenzeller SennenhundOP
I didnt get what you meant
I am really new to next js
Appenzeller SennenhundOP
thanks
ill look in them :)
Ive learned that I can change the router here
thanks again
Western paper wasp
In the pages router, all .js files inside an api folder become “API routes.”

In the app router, only route.js files become “route handlers” regardless of what folder they’re in.

The app router is newer, the future of next.js, and is recommended for new projects.