The http Module
The http module is Node's built-in toolkit for working with the HyperText Transfer Protocol — it can both make outgoing requests and provide the building blocks (request and response objects, status codes, headers) for receiving them.
Learn The http Module in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
This lesson focuses on understanding HTTP and making requests, which is what you'll do most often. We'll keep server creation light and end with a tiny preview, because the very next lesson builds a real server from the ground up.
What You'll Learn in This Lesson
1️⃣ What HTTP Actually Is
HTTP (HyperText Transfer Protocol) is the language clients and servers use to talk over the web. Every interaction is one round trip: a client sends a request , the server sends back a response . Three pieces of vocabulary describe almost everything that happens:
The little script below isn't a network call yet — it just turns that vocabulary into data you can see, so the terms stick before we make a real request.
2️⃣ Making a Request with fetch
Since Node 18, a global fetch() is built in — the same one you may know from the browser, with no import and no install. It's the modern, recommended way to make outgoing requests. Because the network takes time, fetch returns a Promise , so you await it inside an async function.
There are two awaits, and beginners trip on the second one: await fetch(...) gives you a Response object (status, headers, ok ), and a separate await res.json() reads and parses the body. Read the comments closely.
Before fetch existed, you used the http module's own lower-level helpers — http.get(url, cb) for simple GETs and http.request(options, cb) for everything else. They're event-based: you listen for "data" chunks and an "end" event, then assemble the body yourself. They still work and give you fine control, but fetch is far less code for everyday use, so we'll favor it here.
Your turn. The program below works once you fill in the two blanks marked ___ . Follow the 👉 hints, then run it and compare with the expected output.
3️⃣ A Tiny Server Preview
The same http module can also receive requests by creating a server. http.createServer(fn) takes a callback that runs once per incoming request, handing you a request object ( req ) and a response object ( res ) you write the reply into. This is just a teaser — the next lesson is dedicated to building servers properly.
Run it, then hit it from another terminal to see the response come back:
No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it on Node 18+, and check your output against the example in the comments. This is the everyday pattern you'll use constantly.
📋 Quick Reference — http & fetch
Practice quiz
Which HTTP method is typically used to fetch data without changing it?
- GET
- POST
- DELETE
- PUT
Answer: GET. GET expresses the intent to read or fetch a resource.
What does the HTTP status code 404 mean?
- OK
- Server Error
- Not Found
- Created
Answer: Not Found. 404 means the requested resource could not be found.
Which status code means a successful request (OK)?
- 500
- 404
- 201
- 200
Answer: 200. 200 is the standard success status code (OK).
What does res.ok evaluate to for the fetch API?
- True only for 200 exactly
- True for status codes 200 through 299
- True for any response
- True only for 404
Answer: True for status codes 200 through 299. res.ok is true for any status in the 200-299 range.
In Node 18+, how do you make an HTTP request with the built-in fetch?
- require('fetch')
- import fetch from 'node-fetch'
- fetch is a global — call it directly, no import
- http.fetch()
Answer: fetch is a global — call it directly, no import. A global fetch() is built in from Node 18+, with no import or install.
Since fetch returns a Promise, how do you get the response inside an async function?
- You await it
- You loop over it
- You call .then().then()
- You ignore the Promise
Answer: You await it. await pauses for the Promise to resolve to the response object.
Which method parses a JSON response body (and returns a Promise)?
- res.text()
- res.json()
- res.parse()
- JSON.body()
Answer: res.json(). res.json() reads the body stream and parses it as JSON.
Which call creates an HTTP server from the http module?
- http.listen()
- http.newServer()
- http.serve()
- http.createServer()
Answer: http.createServer(). http.createServer(callback) returns a server you then call .listen() on.
Inside the createServer callback, what are the two objects you receive?
- The request (req) and response (res)
- url and method
- headers and body
- socket and port
Answer: The request (req) and response (res). The callback runs per request with (req, res): the request and response objects.
Which method ends a response and sends its body in the http module?
- res.close()
- res.write() only
- res.end()
- res.send()
Answer: res.end(). res.end(body) finishes the response; res.send() is Express, not core http.