Complete the code to define a GET handler in Next.js API route.
export async function [1](request) { return new Response('Hello from GET'); }
The GET handler is named GET in Next.js API routes to respond to GET requests.
Complete the code to define a POST handler that reads JSON body in Next.js API route.
export async function POST(request) {
const data = await request.json();
return new Response(JSON.stringify({ message: data.[1] }), { status: 200 });
}The POST handler reads the JSON body and accesses the name property from it.
Fix the error in the POST handler to correctly parse JSON body.
export async function POST(request) {
const data = request.[1]();
return new Response(JSON.stringify(data), { status: 200 });
}The json() method must be called with parentheses to return a promise that resolves to the parsed JSON.
Fill both blanks to create a GET handler that returns JSON with status 200.
export async function GET(request) {
const response = new Response(JSON.stringify({ message: 'Hello' }), { status: [1] });
response.headers.set('Content-Type', [2]);
return response;
}Status code 200 means success. The Content-Type header must be 'application/json' to indicate JSON data.
Fill all three blanks to create a POST handler that reads JSON, extracts 'title', and returns it with status 201.
export async function POST(request) {
const data = await request.[1]();
const title = data.[2];
return new Response(JSON.stringify({ received: title }), { status: [3] });
}The POST handler calls json() to parse the body, extracts the 'title' property, and returns a response with status 201 indicating resource creation.