A Route Handler exports only GET and POST. What do HEAD, OPTIONS, and DELETE requests get?
The tea shop's stock API, app/api/stock/route.ts, exports a GET that answers how many boxes of a tea are left, and a POST that adds boxes. Each logs the method of the request it ran for. A program sends it four requests. What does each one get, and what does the server print?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
app/api/stock/route.tsimport type { NextRequest } from "next/server";
const boxes: Record<string, number> = { green: 12, mint: 0, oolong: 4 };
export async function GET(request: NextRequest) {
const tea = request.nextUrl.searchParams.get("tea") ?? "";
console.log(`GET ran for a ${request.method} request`);
return Response.json({ tea, boxes: boxes[tea] ?? 0 });
}
export async function POST(request: NextRequest) {
const { tea, added } = await request.json();
console.log(`POST ran for a ${request.method} request`);
boxes[tea] = (boxes[tea] ?? 0) + Number(added);
return Response.json({ tea, boxes: boxes[tea] }, { status: 201 });
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:
- You run
next buildandnext start. - A program sends
GET /api/stock?tea=oolong. - It sends
HEAD /api/stock?tea=oolong. - It sends
OPTIONS /api/stock. - It sends
DELETE /api/stock?tea=oolong.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…