Modify a Request in Next.js API Route
📖 Scenario: You are building a Next.js API route that receives a JSON request with a user's name and age. You want to modify the request data by adding a new field before sending the response.
🎯 Goal: Create a Next.js API route that reads the incoming JSON request body, adds a new field isAdult based on the age, and returns the modified JSON object.
📋 What You'll Learn
Create an API route handler function named
handler that accepts req and res parametersRead the JSON body from
req.body containing name and ageAdd a new boolean field
isAdult which is true if age is 18 or more, otherwise falseReturn the modified object as JSON in the response using
res.status(200).json()💡 Why This Matters
🌍 Real World
API routes in Next.js are used to handle backend logic like processing form data, modifying requests, and sending responses in web applications.
💼 Career
Understanding how to modify request data and send JSON responses is essential for backend development in Next.js, a popular React framework used in many companies.
Progress0 / 4 steps