Script Valley
Next.js: Full-Stack React Applications
API Routes and Server Actions/Assessment

Practice & Assessment

Test your understanding of API Routes and Server Actions

Multiple Choice Questions

5
1

You define both page.tsx and route.ts in the same folder. What happens?

2

Where should 'use server' be placed to mark a Server Action?

3

What does revalidatePath('/blog') do when called inside a Server Action?

4

Using Zod in a Server Action, what does safeParse return when validation fails?

5

In a Route Handler for /api/posts/[id], how do you access the id param?

Coding Challenges

1
1

Build a CRUD REST API for a Notes App

Create Route Handlers for a notes API using an in-memory array as the data store (no database needed). Implement: GET /api/notes (returns all notes as JSON array), POST /api/notes (accepts { title, body } JSON body, adds to array, returns created note with generated id, status 201), GET /api/notes/[id] (returns single note or 404), DELETE /api/notes/[id] (removes note, returns 204). Use Zod to validate the POST body — title must be a non-empty string, body must be at least 5 characters. Return field-level errors as { errors: { title: string[], body: string[] } } on validation failure with status 400. Test with curl or a REST client. Time estimate: 25-30 minutes.

Medium

Mini Project

1

Full-Stack Contact Form with Admin Dashboard

Build a contact form application using Server Actions and Route Handlers. Requirements: public /contact page with a form (name, email, message fields); Server Action createContact that validates input with Zod (name min 2 chars, valid email format, message min 20 chars) and stores to an in-memory store (or JSON file); use useActionState to show validation errors per field and a success state; GET /api/contacts Route Handler returning all submissions as JSON with an x-total-count header; /admin/contacts Server Component that fetches from the Route Handler and displays all submissions in a table; middleware that protects /admin routes by checking for an admin-secret cookie (redirect to /login if missing); a /login page with a form action that sets the cookie. All mutations use Server Actions. All data display uses Server Components.

Medium