0
0
Node.jsframework~10 mins

Creating a basic HTTP server in Node.js - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating a basic HTTP server
Start Node.js
Import http module
Create server with request handler
Server listens on port
Client sends request
Server handles request
Server sends response
Client receives response
End
This flow shows how Node.js starts, creates an HTTP server, listens for requests, handles them, and sends responses back to clients.
Execution Sample
Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(3000);
This code creates a simple HTTP server that responds with 'Hello World' to every request on port 3000.
Execution Table
StepActionEvaluationResult
1Import http modulehttp module loadedhttp object available
2Create server with request handlerFunction passed to createServerserver object created
3Start server listening on port 3000server.listen(3000)Server waits for requests
4Client sends requestRequest received by serverRequest object (req) created
5Server handles requestExecute handler functionSet statusCode=200, set header
6Server sends responseres.end('Hello World')Response sent to client
7Client receives responseResponse with 'Hello World'Client displays 'Hello World'
8No more requestsServer keeps runningWaiting for next request
💡 Server runs continuously until manually stopped; no exit in normal flow
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
httpundefinedhttp module objecthttp module objecthttp module objecthttp module objecthttp module object
serverundefinedserver object createdserver listeningserver listeningserver listeningserver listening
requndefinedundefinedundefinedrequest objectrequest objectundefined
resundefinedundefinedundefinedresponse objectresponse sentundefined
Key Moments - 3 Insights
Why does the server keep running after sending a response?
Because server.listen keeps the Node.js process alive waiting for more requests, as shown in execution_table step 8.
What happens if we don't call res.end()?
The response never finishes, so the client waits forever. Step 6 shows res.end sends the response and ends it.
Why do we set headers before ending the response?
Headers must be sent before the body. Step 5 sets headers, then step 6 ends response with body.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the statusCode set to in step 5?
A200
B404
C500
DUndefined
💡 Hint
Check step 5 in execution_table where server handles request and sets statusCode
At which step does the server start listening for requests?
AStep 5
BStep 2
CStep 3
DStep 7
💡 Hint
Look at execution_table step 3 where server.listen is called
If we remove res.end(), what happens to the client response?
AClient receives 'Hello World' anyway
BClient waits forever for response to finish
CServer crashes immediately
DResponse is sent but with empty body
💡 Hint
Refer to key_moments explanation about importance of res.end() after step 6
Concept Snapshot
Create HTTP server with http.createServer(handler).
Handler gets request and response objects.
Set statusCode and headers on response.
Call res.end() to send response body.
Start server with server.listen(port).
Server runs continuously until stopped.
Full Transcript
This lesson shows how to create a basic HTTP server in Node.js. First, we import the http module. Then, we create a server using http.createServer and provide a function that handles requests and responses. Inside this function, we set the response status code to 200, set the Content-Type header to text/plain, and end the response with 'Hello World'. We start the server listening on port 3000. When a client sends a request, the server runs the handler, sends the response, and the client receives 'Hello World'. The server keeps running waiting for more requests until manually stopped.