Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Next.js Response Formatting
📖 Scenario: You are building a simple Next.js API route that returns user data in a formatted JSON response. This is common when creating backend endpoints for web apps.
🎯 Goal: Create a Next.js API route that returns a JSON response with a status field and a data field containing user information.
📋 What You'll Learn
Create a Next.js API route handler function named handler
Define a user object with exact fields: id, name, and email
Create a response object with status set to success and data containing the user object
Return the response using Next.js res.status(200).json() method
💡 Why This Matters
🌍 Real World
API routes in Next.js are used to build backend endpoints that send JSON data to frontend apps or other services.
💼 Career
Understanding how to format and send JSON responses in Next.js API routes is essential for full-stack web development roles.
Progress0 / 4 steps
1
Create user data object
Create a constant called user with these exact fields and values: id set to 1, name set to "Alice", and email set to "alice@example.com".
NextJS
Hint
Use const user = { id: 1, name: "Alice", email: "alice@example.com" } to create the object.
2
Create response object
Create a constant called response that is an object with status set to "success" and data set to the user object.
NextJS
Hint
Use const response = { status: "success", data: user } to create the response.
3
Create API route handler function
Create an exported async function called handler that takes req and res as parameters.
NextJS
Hint
Use export default async function handler(req, res) { } to define the API route.
4
Send JSON response
Inside the handler function, send the response object as JSON with HTTP status 200 using res.status(200).json(response).
NextJS
Hint
Use res.status(200).json(response) to send the JSON response.
Practice
(1/5)
1. What is the main purpose of setting the Content-Type header in a Next.js API response?
easy
A. To set the status code of the response
B. To specify the HTTP method used in the request
C. To define the URL path of the API endpoint
D. To tell the client what type of data is being sent
Solution
Step 1: Understand the role of headers in HTTP responses
Headers provide metadata about the response, including data format.
Step 2: Identify the purpose of Content-Type
This header tells the client how to interpret the response body, e.g., JSON or HTML.
Final Answer:
To tell the client what type of data is being sent -> Option D
Quick Check:
Content-Type = Data format info [OK]
Hint: Content-Type always describes the data format sent [OK]
Common Mistakes:
Confusing Content-Type with HTTP method
Thinking Content-Type sets status code
Mixing URL path with headers
2. Which of the following is the correct way to create a JSON response with status 200 in a Next.js API route?
easy
A. return new Response(data, { statusCode: 200, contentType: 'application/json' })
B. return Response(data, 200, { 'Content-Type': 'application/json' })
C. return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } })
D. return new Response(JSON.stringify(data), 200, { 'Content-Type': 'text/plain' })
Solution
Step 1: Check the Response constructor syntax
It takes the body as first argument, and an options object with status and headers.
Step 2: Verify correct headers and status usage
Headers must include 'Content-Type' with 'application/json' for JSON data, and status should be 200.
Final Answer:
return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } }) -> Option C