All quizzesMedium
API Routes & Middleware — Series 3
Preview — 3 of 10 questions
What is the correct way to read the request body and send back a created resource?
javascript
// app/api/comments/route.ts
export async function POST(request: Request) {
// read the JSON body, create the comment, respond
}Aconst body = request.body — it is already a parsed object
Bconst body = JSON.parse(request) then return request.send(body)
Cconst body = await request.json(), then return NextResponse.json(comment, { status: 201 }) (or Response.json(...))
DRoute Handlers cannot read a request body — use a Server Action instead
In a fresh Next.js 15 project, you deploy this and hit /api/now repeatedly. What do you observe by default?
javascript
// app/api/now/route.ts
export async function GET() {
return Response.json({ time: Date.now() });
}AThe timestamp updates on every request — in Next.js 15, GET Route Handlers are not cached by default; you opt into caching with export const dynamic = 'force-static' or a revalidate value
BThe timestamp is frozen at build time forever, with no way to change it
CThe handler runs once per deployment and the result is cached until the next deploy
DGET handlers always error unless you add export const dynamic = 'force-dynamic'
What does router.refresh() do here?
javascript
'use client';
import { useRouter } from 'next/navigation';
function DeleteButton({ id }: { id: string }) {
const router = useRouter();
async function onDelete() {
await fetch(`/api/items/${id}`, { method: 'DELETE' });
router.refresh();
}
return <button onClick={onDelete}>Delete</button>;
}AIt performs a full browser reload of the page (location.reload())
BIt re-runs the Server Components for the current route on the server and merges the new RSC payload into the page, without clearing client-side React state (like input values or scroll position) or unmounting Client Components
CIt navigates to the same URL, remounting the entire tree from scratch
DIt only clears the Next.js Data Cache and has no visible effect until the next navigation
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.