← Next.js learning path
Next.js · Module 5 of 10

Route handlers and Server Actions

Build backend endpoints and handle form submissions.

📖 Key ideas

  1. Route handlers: app/api/hello/route.ts exporting GET or POST.
  2. Return Response.json({ ok: true }).
  3. Server Actions: async functions with "use server" called from forms.
  4. <form action={createPost}> submits without writing an API.
  5. Always validate input and check auth inside actions.
💡 Real-life example: A Server Action is a direct phone line from the form to the kitchen, no waiter in between.
route.tsAPI endpoint file
Server ActionServer function called from the client
"use server"Marks server actions
Response.jsonReturns JSON from a handler