Back to results for “Handlers”
Next.js › Route Handlers (API Route.js)

What is a route handler in the Next.js App Router, and how do you create one?

Beginner

Answer

A route handler is a file named route.js placed inside a folder within your app directory, exporting functions named after HTTP methods like GET or POST, letting you build backend API endpoints directly within the App Router, similar to what API routes did in the Pages Router.
// app/api/hello/route.js
export async function GET() {
  return Response.json({ message: 'Hello from a route handler' });
}
Real-world example A weather application builds a simple route handler that returns current weather data as JSON, accessible at the api/hello path within its App Router based project.

Common follow-ups: Can a single route.js file export functions for multiple HTTP methods?;What is the difference between a route handler and a server action?