← Next.js learning pathNext.js · Module 5 of 10
Route handlers and Server Actions
Build backend endpoints and handle form submissions.
📖 Key ideas
- Route handlers: app/api/hello/route.ts exporting GET or POST.
- Return Response.json({ ok: true }).
- Server Actions: async functions with "use server" called from forms.
- <form action={createPost}> submits without writing an API.
- 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