Node.js Course
Node.js
/
Intermediate

Request & Response Lifecycle

Definition

The end-to-end journey of a network call, from the client establishing a connection, to the server parsing the request, executing business logic, and terminating the connection by sending a response.

Explain Like I'm New

A conversation. 1. Client calls the server: 'Hello, here is my cookie (Headers) and here is a photo (Body).' 2. Server parses the data. 3. Server talks to the database. 4. Server replies: 'Here is status code 201 (Headers) and a success message (Body).' 5. The server hangs up the phone (ends the connection).

Real World Example

If your code forgets to call `response.end()` (or `res.send()` in Express), the server literally never hangs up the phone. The user's browser will show a spinning loading wheel forever until it eventually times out.

Common Use Cases

  • •Debugging infinite loading spinners
  • •Parsing JSON bodies

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What happens if you never call `.end()` on a response?

intermediate

  • Can you send headers AFTER you have already sent the body?

Flash Cards

Question

Never call end()?

Click to reveal answer
Answer

The client's browser will hang infinitely waiting for the server to finish.

Question

Headers after body?

Click to reveal answer
Answer

No! Headers are the envelope; the body is the letter. You cannot write on the envelope after you have already mailed the letter. Node will throw an 'ERR_HTTP_HEADERS_SENT' crash if you try to `res.writeHead` after `res.write`.