nextjs api route

Step 1: Create a Next.js Project

npx create-next-app my-nextjs-app
cd my-nextjs-app

Step 2: Create an API Route Create a new file under the pages/api directory, for example, pages/api/hello.js.

Step 3: Define the API Route Inside the newly created file (hello.js), define your API route using the Next.js API route structure.

// pages/api/hello.js
export default function handler(req, res) {
  res.status(200).json({ text: 'Hello' })
}

Step 4: Run the Next.js Development Server

npm run dev

Step 5: Access the API Route Visit http://localhost:3000/api/hello in your browser or make a request to the API route using tools like cURL or Postman.

Step 6: Check the Response You should see a JSON response with the text "Hello" as defined in the API route handler.