Typorithm · easy

http server

http-servernodejshttprequestresponse
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn http server

0%

HTTP & Events

createServer from node:http builds an HTTP server from a handler. The handler gets a request (req) and a response (res); you set headers with res.writeHead and send the body with res.end, then server.listen binds a port.

node:http is built in: createServer returns a server whose handler runs once per request, receiving the incoming req and the outgoing res.

req carries the method, url, and headers of the request; res is how you reply — writeHead sets the status and headers, and end sends the body.

Nothing reaches the client until you call res.end; server.listen(3000) then binds the port so the server starts accepting connections.

01

createServer takes a (req, res) handler.

02

res.writeHead sets status code and headers.

03

res.end sends the body and closes the response.

import { createServer } from "node:http"

const server = createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" })
  res.end("Hello")
})

server.listen(3000)

Read the implementation

Connect each part of the code to the concept before you type.

1. Import createServer

createServer comes from the built-in node:http module; no package install is needed.

import { createServer } from "node:http"

2. Create the server

createServer takes a handler that runs on every incoming request, receiving the request and response objects.

const server = createServer((req, res) => {

3. Write headers

writeHead sends the status code and response headers before any body is written.

res.writeHead(200, { "Content-Type": "text/plain" })

4. End the response

res.end writes the final body chunk and closes the response so the client receives it.

res.end("Hello")

5. Listen on a port

listen binds the server to port 3000 and starts accepting connections.

server.listen(3000)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

http server: Lesson & Practice · Typorithm | ApplyDjinn