API Routes in NextJS
December 21, 2024Less than 1 minute
API Routes in NextJS
Craete a folder named api
in the pages
folder directory of your project.
Inside the api
folder, create a new file named hello.js
.
export default function handler(req, res) {
res.status(200).json({ message: 'Hello from API!' });
}
This is a simple API route that returns a JSON object with a message.
To use this API route, you can make a GET request to http://localhost:3000/api/hello
. The response will be a JSON object with the message "Hello from API!". if you want send a request from the front end, the url should be api/hello.