API giving a 404 error
Answered
Asian black bear posted this in #help-forum
Asian black bearOP
I am testing the API feature in next js and it keeps giving me a 404 error.
My files are in the following dir: app/pages/api/hello.js
The following url: http://localhost:3000/api/hello
here is the code in my hello.js file:
export default function handler(req, res) {
res.status(200).json({ text: 'Hello' })
}
Thanks in advance!
My files are in the following dir: app/pages/api/hello.js
The following url: http://localhost:3000/api/hello
here is the code in my hello.js file:
export default function handler(req, res) {
res.status(200).json({ text: 'Hello' })
}
Thanks in advance!
Answered by Ray
// app/api/hello/route.ts
export async function GET() {
return Response.json({ hello: 'world' })
}5 Replies
@Asian black bear I am testing the API feature in next js and it keeps giving me a 404 error.
My files are in the following dir: app/pages/api/hello.js
The following url: http://localhost:3000/api/hello
here is the code in my hello.js file:
export default function handler(req, res) {
res.status(200).json({ text: 'Hello' })
}
Thanks in advance!
either move the
pages folder to root. it should be same level as app or create a route hander in app/api/hello/route.ts// app/api/hello/route.ts
export async function GET() {
return Response.json({ hello: 'world' })
}Answer
@Ray either move the `pages` folder to root. it should be same level as `app` or create a route hander in `app/api/hello/route.ts`
Asian black bearOP
I really appreciate it
been stuck on this for a bit
IT WORKS!