Recall & Review
beginner
What is the purpose of the GET method handler in Next.js API routes?
The GET method handler responds to client requests to retrieve data. It usually sends back information without changing anything on the server.
Click to reveal answer
beginner
How does the POST method handler differ from GET in Next.js API routes?
The POST method handler receives data from the client to create or update resources on the server. It changes server state, unlike GET which only fetches data.
Click to reveal answer
intermediate
Show a simple example of a Next.js API route handling GET and POST methods.
export async function GET(request) {
return new Response('Hello from GET!');
}
export async function POST(request) {
const data = await request.json();
return new Response(`Received: ${JSON.stringify(data)}`);
}Click to reveal answer
intermediate
Why should you define separate handlers for different HTTP methods in Next.js API routes?
Defining separate handlers for different HTTP methods ensures your API responds correctly to different types of requests, like GET for fetching data and POST for sending data. It helps avoid errors and improves security.
Click to reveal answer
beginner
What happens if you send a POST request to a Next.js API route that only has a GET handler?
The server will respond with a 405 Method Not Allowed error because the POST method is not handled in that route.
Click to reveal answer
Which HTTP method is typically used to retrieve data without changing the server state?
✗ Incorrect
GET requests fetch data without modifying anything on the server.
In Next.js API routes, how do you access the data sent in a POST request?
✗ Incorrect
You use await request.json() to parse the JSON data sent in the POST request body.
What status code does the server return if an unsupported HTTP method is used?
✗ Incorrect
405 Method Not Allowed means the HTTP method is not supported by the API route.
Which Next.js function handles GET requests in an API route?
✗ Incorrect
GET requests are handled by the exported async function named GET.
Why is it important to handle different HTTP methods separately in Next.js API routes?
✗ Incorrect
Handling methods separately ensures the API responds properly to different client requests.
Explain how GET and POST method handlers work in Next.js API routes and when to use each.
Think about when you want to get information versus when you want to send information.
You got /4 concepts.
Describe what happens if a client sends a POST request to a Next.js API route that only has a GET handler.
Consider how the server reacts to unsupported methods.
You got /4 concepts.