Next.js · Junior · Question 63 of 75
How do you handle POST /api/posts in a Route Handler?
// app/api/posts/route.ts
export async function POST(request: Request) {
const body = await request.json()
return Response.json(body, { status: 201 })
}- ✓Correct answer: Export an async function named
POSTfromroute.ts - 2Export a default function and check
request.method - 3Create a file named
post.ts
Explanation
Each HTTP method is a named export: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS. Unsupported methods get a 405 response.