0
0
NextJSframework~10 mins

Why API routes serve backend logic in NextJS - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why API routes serve backend logic
Client sends HTTP request
Next.js API Route receives request
Backend logic runs (DB, auth, calculations)
API Route sends response back
Client receives data or confirmation
This flow shows how a client request goes to a Next.js API route, which runs backend code and sends back a response.
Execution Sample
NextJS
export default function handler(req, res) {
  if (req.method === 'GET') {
    res.status(200).json({ message: 'Hello from backend!' });
  }
}
This API route responds to GET requests with a JSON message from backend logic.
Execution Table
StepActionRequest MethodBackend LogicResponse Sent
1Receive requestGETCheck methodNo response yet
2Check methodGETMatches GET branchNo response yet
3Send responseGETPrepare JSON message200 OK with { message: 'Hello from backend!' }
4EndNo new requestNo logicResponse sent, connection closed
💡 Request handled and response sent, no further action.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
req.methodundefinedGETGETGETGET
res.statusCodeundefinedundefinedundefined200200
res.bodyundefinedundefinedundefined{"message":"Hello from backend!"}{"message":"Hello from backend!"}
Key Moments - 2 Insights
Why does the API route check req.method before responding?
Because the API route can handle different HTTP methods (GET, POST, etc.), it checks req.method to run the correct backend logic. See execution_table step 2 where it matches 'GET'.
Is the API route code running on the client or server?
It runs on the server. The client sends a request, and the API route runs backend logic before sending a response, as shown in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the response sent?
A200 OK with JSON message
B404 Not Found
C500 Internal Server Error
DNo response sent yet
💡 Hint
Check the 'Response Sent' column at step 3 in execution_table.
At which step does the API route check the HTTP method?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Backend Logic' column in execution_table to find where method is checked.
If the request method was POST instead of GET, what would happen in this code?
AThe API route would send the same JSON response
BThe API route would do nothing and no response sent
CThe API route would not match GET branch and send no response
DThe API route would throw an error
💡 Hint
Refer to the code and execution_table step 2 where only GET is handled.
Concept Snapshot
Next.js API routes handle backend logic by receiving HTTP requests.
They check the request method (GET, POST, etc.) to decide what to do.
Backend code runs on the server inside the API route function.
The route sends back a response (JSON, status) to the client.
This lets frontend and backend live together in one app.
Full Transcript
In Next.js, API routes serve backend logic by acting as server-side functions that respond to HTTP requests. When a client sends a request, the API route receives it and checks the HTTP method, like GET or POST. Based on the method, it runs backend code such as fetching data or processing input. Then it sends a response back to the client, often JSON data. This flow allows backend logic to live inside the Next.js app, separate from frontend UI code. The example code shows a GET request handler that returns a JSON message. The execution table traces each step: receiving the request, checking the method, preparing the response, and sending it. Variables like req.method and res.statusCode change as the code runs. Beginners often wonder why method checking is needed and where the code runs; it runs on the server and method checking directs the logic. The visual quiz tests understanding of these steps and outcomes.