Skip to content
Learn Netverks

Lesson

Step 23/36 64% through track

route-handlers

Route Handlers

Last reviewed May 28, 2026 Content v20260528
Track mode
client_nextjs
Means
In-browser Next.js (client components)
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Route Handlers: the concepts, APIs, and habits you need before advancing in Next.js.

Routing maps URLs to components—guards and resolvers protect enterprise flows.

You will apply Route Handlers in contexts like: Marketing sites, dashboards, e-commerce, and Vercel-style deployments that need hybrid static + dynamic pages.

Write TSX for Client Components, click Run—React 18 CDN + in-browser TSX compile; use client/server lessons explain App Router concepts; mountApp renders interactive UI; printOutput feeds the terminal.

When hooks, state, and effects from intermediate lessons are familiar.

Route Handlers define HTTP endpoints in route.ts files—App Router’s replacement for Pages Router API routes. They run on the server and can return JSON, streams, webhooks, and more.

Basic GET handler

app/api/posts/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  const posts = await db.post.findMany();
  return NextResponse.json(posts);
}

export async function POST(request: Request) {
  const body = await request.json();
  const created = await db.post.create({ data: body });
  return NextResponse.json(created, { status: 201 });
}

When to use

  • REST/JSON APIs for mobile apps or external consumers
  • Webhooks (Stripe, GitHub)
  • Operations that must not ship logic to the client

Server Actions vs Route Handlers

Prefer Server Actions for form mutations from your own UI. Use Route Handlers when you need standard HTTP semantics or non-React clients.

Self-check

  1. What file name defines an API route in App Router?
  2. When would you pick a Route Handler over a Server Action?

Interview prep

Where do Route Handlers live?

route.ts in an app/ segment—export GET, POST, etc. returning NextResponse.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Handler vs Server Action?
  • REST from app/api?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump