What is API Routed in Next JS and How it works

Next.js has support for API Routes, which let you easily create an API endpoint as a Node.js serverless function

API Routes let you create an API endpoint inside a Next.js app. You can do so by creating a function inside the pages/api directory.

Let’s try it out. Create a file called hello.js in pages/api with the following code:

export default function handler(req, res) {
  res.status(200).json({ text: 'Hello' });
}

Try accessing it at http://localhost:3000/api/hello. You should see {"text":"Hello"}.

Leave a comment

Your email address will not be published. Required fields are marked *