Next.js Course
Next.js
/
Intermediate

Route Handlers

Definition

The App Router equivalent of API Routes. They allow you to create custom REST API endpoints directly within your Next.js application.

Explain Like I'm New

You don't need to build a separate Express.js backend. You create a file named `route.ts`. If someone makes an HTTP request to that folder's URL, your `route.ts` file acts as the backend server, returning JSON data instead of HTML.

Real World Example

Building an endpoint at `/api/webhooks/stripe` that listens for payment success messages from Stripe's servers.

Common Use Cases

  • •Building public APIs
  • •Handling Webhooks
  • •Mobile app backends

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What exact file name must be used to create an API endpoint in the App Router?

intermediate

  • Can you put a `page.tsx` and a `route.ts` in the exact same folder?

Flash Cards

Question

File name?

Click to reveal answer
Answer

`route.ts` (or `.js`).

Question

In the same folder?

Click to reveal answer
Answer

No! A folder represents a single URL. A URL cannot simultaneously return an HTML webpage (`page.tsx`) AND raw JSON data (`route.ts`). They will conflict. API routes usually live in an `app/api/...` folder structure.