Help with api routing app/api/{file} doesnt work for some reason
Answered
Appenzeller Sennenhund posted this in #help-forum
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
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]
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]
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 Sennenhund I didnt get what you meant
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Take a look of the examples here
Take a look of the examples here
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
In the app router, only
The app router is newer, the future of next.js, and is recommended for new projects.
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.