Next.js Discord

Discord Forum

API routes returning 404

Unanswered
Yellowstripe scad posted this in #help-forum
Open in Discord
Yellowstripe scadOP
After setting up nextjs latest version, it didn't contain any api folder so i created one in app and added a file within it.
The problem is that when I try to access that route via localhost:3000/api/parsecsv i'm getting 404 page not found.

localhost:3000 works fine.

here is the repo:
https://github.com/yanndine/prospect

17 Replies

@joulev it needs to be `api/parsecsv/route.ts` not `api/parsecsv.ts`
Yellowstripe scadOP
do you mean that every api route that i want to create should be a folder within app/api that has a route.ts containing the code for that route ?
@joulev yup
Yellowstripe scadOP
that's weird.. but thank you very much for your help, it works now.
that's how the app folder works
completely different from the pages folder
@joulev https://tenor.com/view/goblin-slayer-stressed-priestess-anime-you-get-used-to-it-gif-13752108
Yellowstripe scadOP
are there use cases where that folder will contain something else than route.ts ?
otherwise it's just seems inefficient (but what do i know 🤷‍♂️ )
@Yellowstripe scad are there use cases where that folder will contain something else than route.ts ? otherwise it's just seems inefficient (but what do i know 🤷‍♂️ )
you can store utility functions inside there for example
app/api/hello
  get-name.ts
  route.ts

// get-name.ts
export function getName() {
  return "Yann";
}

// route.ts
export function GET() {
  return NextResponse.json({ name: getName() });
}
colocation is a major part of the app router design, you can read about it here https://nextjs.org/docs/app/building-your-application/routing/colocation; it is also applied for route handlers
@joulev colocation is a major part of the app router design, you can read about it here <https://nextjs.org/docs/app/building-your-application/routing/colocation>; it is also applied for route handlers
Yellowstripe scadOP
got it makes sense. thanks again for the help, you're the best.
By the way, if you have some free time, i've just launched a product https://insite.chat would love to have your feedback and I can give you a pro account for free if you need it
@Yellowstripe scad got it makes sense. thanks again for the help, you're the best. By the way, if you have some free time, i've just launched a product https://insite.chat would love to have your feedback and I can give you a pro account for free if you need it
woah that's cool. i dont really need this functionality right now, but i'll keep this in mind, thanks. btw i do recommend you to introduce this in #showcase, it will reach more people that way
@joulev woah that's cool. i dont really need this functionality right now, but i'll keep this in mind, thanks. btw i do recommend you to introduce this in <#771729272074534922>, it will reach more people that way
Yellowstripe scadOP
thank you very much. I should train a chatbot on nextjs documentation and try to solve every problem with it haha since chatgpt is not trained on latest versions
yeah that's a good idea
@joulev yeah that's a good idea
Yellowstripe scadOP
btw I'm building a new product, if you're interested in joining a new project, would love to pitch it to you
mmm appreciate it but im currently not available for a new job, a bit too busy already