NextJS example API responds with error 500 [Resolved]
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
I receive an error 500 using the example API script on the NextJS website (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>
) {
if (res) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
}12 Replies
Check to see if are using pages router or the app router in your app.
Using this code in app router won't work
@Transvaal lion I receive an error 500 using the example API script on the NextJS website (https://nextjs.org/docs/pages/building-your-application/routing/api-routes)
ts
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
if (res) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
}
In the left sidebar, at the top, change from page to app router
@Clown Using this code in app router won't work
Transvaal lionOP
so how do i use API endpoints with app router?
do i just make a
pages folder and put my api stuff in there?@Transvaal lion so how do i use API endpoints with app router?
Let me send you the link
Transvaal lionOP
alright
@"use php" https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Transvaal lionOP
thank you, i will have a look at this
@"use php" https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Mark message as solution
Right Click > Apps > Mark Solution