API Routes are not working? Even with official docs examples.
Answered
6vz posted this in #help-forum
6vzOP
I really don't know and I really want to just give up on this. I am fighting with trying to create simple API endpoint that will ask Database (Prisma w/ pSQL) to get one row. But no matter what i try, i always get simmilar error.
I kinda have enough of this - Every frickin' time, I'm getting
Here's my code I've copied from official Next.js docs about API Routes.
https://nextjs.org/docs/pages/building-your-application/routing/api-routes
Maybe I am doing something wrong and not noticing it?
If so - I will be more than happy if someone can point me to correct solutions.
Thanks for any help 🙂
app/api/page.ts (11:6) @ status
9 | res: NextApiResponse<ResponseData>
10 | ) {
> 11 | res.status(200).json({ message: 'Hello from Next.js!' })
| ^
12 | }I kinda have enough of this - Every frickin' time, I'm getting
Error: Cannot read properties of undefined (reading 'status') like res has nothing inside of it. This starts to piss me off.Here's my code I've copied from official Next.js docs about API Routes.
https://nextjs.org/docs/pages/building-your-application/routing/api-routes
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ message: 'Hello from Next.js!' })
}Maybe I am doing something wrong and not noticing it?
If so - I will be more than happy if someone can point me to correct solutions.
Thanks for any help 🙂
Answered by Asian black bear
The api in the app dir is entirely different and the
res object is in fact not there.6 Replies
Asian black bear
The api in the app dir is entirely different and the
res object is in fact not there.Answer
Asian black bear
They are called route handlers and you need to follow the docs here, and there are some examples. https://nextjs.org/docs/app/building-your-application/routing/route-handlers
6vzOP
Lemme check if this works for me.
If yes, gonna marked it as solved
@6vz Lemme check if this works for me.
also, the file name should be route.ts
/api/page/route.tsAsian black bear
also the /api part is completely unnecessary. Any path with route.ts is an api route.