0
0
Expressframework~10 mins

res.send for general responses in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - res.send for general responses
Client sends HTTP request
Express receives request
Route handler runs
Call res.send(data)
Express sets HTTP response body
Express sends response to client
Client receives response
This flow shows how Express handles a client request and sends a response using res.send.
Execution Sample
Express
app.get('/hello', (req, res) => {
  res.send('Hello, friend!');
});
This code sends a simple text response 'Hello, friend!' when the client visits /hello.
Execution Table
StepActionInput/ValueResult/Output
1Client sends GET request to /helloGET /helloRequest received by Express
2Express matches route /hello/helloRoute handler function called
3res.send called with 'Hello, friend!''Hello, friend!'Response body set to 'Hello, friend!'
4Express sends HTTP responseResponse body: 'Hello, friend!'Client receives response with status 200 and body
5Request handling complete-Connection closed or kept alive
💡 Response sent to client, request cycle ends
Variable Tracker
VariableStartAfter res.send
res._sentfalsetrue
res.bodyundefined'Hello, friend!'
Key Moments - 3 Insights
Why does the client see 'Hello, friend!' after res.send is called?
Because res.send sets the response body and sends it immediately, as shown in execution_table step 3 and 4.
Can res.send send different types of data?
Yes, res.send can send strings, buffers, JSON objects, or arrays. Express converts them properly before sending.
What happens if res.send is called twice?
Only the first call sends the response. Subsequent calls are ignored or cause errors, since res._sent becomes true after the first call.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response body set to at step 3?
Aundefined
B'Goodbye!'
C'Hello, friend!'
Dnull
💡 Hint
Check the 'Input/Value' column at step 3 in the execution_table.
At which step does Express send the response back to the client?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where 'Express sends HTTP response' in the 'Action' column.
If res.send is called twice, what happens according to variable_tracker?
Ares._sent stays false
Bres._sent becomes true after first call and second call is ignored
Cres.body changes to second value
DExpress crashes immediately
💡 Hint
See how res._sent changes from false to true after res.send in variable_tracker.
Concept Snapshot
res.send(data) sends HTTP response body to client.
Accepts strings, buffers, JSON, arrays.
Sends response immediately and ends request.
Only first res.send call works per request.
Used in route handlers to reply to requests.
Full Transcript
When a client sends a request to an Express server, Express finds the matching route handler. Inside the handler, calling res.send with some data sets the HTTP response body and sends it back to the client immediately. The client then receives this response. res.send can send different data types like strings or JSON. Only the first call to res.send sends the response; calling it again does nothing because the response is already sent. This process is shown step-by-step in the execution table and variable tracker.