Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

NO
Node.js & Express
0/13 completed
Beginner
What is Node.js?Modules and npmWorking with filesCallbacks, promises and the event loop
Intermediate
Events and streamsYour first HTTP serverExpress basicsRouting and middlewareConnecting a database
Advanced
Validation and configurationAuthentication with JWTTesting APIs
Pro
Deploying Node apps to production
Tracks / Node.js & Express / Intermediate

Your first HTTP server

Every website and API you use is a server: a program that waits for requests and sends back responses. Node has a web server built in, the node:http module. Frameworks like Express are built on top of it, so seeing it raw first makes everything later easier to understand.

Request in, response out

When you open http://localhost:3000/hello in a browser, it sends a request like this:

http
GET /hello HTTP/1.1
Host: localhost:3000
Accept: text/html

and the server replies:

http
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8

Hello, world!

A request has a method (GET, POST…), a URL path, headers and sometimes a body. A response has a status code (200 OK, 404 Not Found…), headers and a body.

A minimal server

Node.js · editable
  • createServer takes a request handler that Node calls for every request, with a request object (req) and a response object (res).
  • listen(3000) starts listening on port 3000. The callback runs once the server is ready.
  • res.end(body) sends the response. Every request must end with a response, or the browser waits until it times out.

On your computer, leave out the fetch and server.close() lines, run node server.js, and open http://localhost:3000/hello in your browser. The server keeps running until you press Ctrl+C. In these editors we call our own server with fetch and then close it so the example finishes.

Routing by hand

A real server responds differently depending on the method and the path. That's called routing:

Node.js · editable

new URL(req.url, base) splits the URL into pathname and searchParams (the query string after ?).

Reading a request body

POST and PUT requests carry data in their body. The request is a readable stream, so the body arrives in chunks that we collect:

Node.js · editable

Status codes and headers

Code Meaning Use it when
200 OK Success Returning data
201 Created A new resource was created After a successful POST
204 No Content Success, nothing to return After a DELETE
400 Bad Request The client sent something invalid Malformed JSON, missing fields
401 Unauthorized Not logged in Missing or bad credentials
403 Forbidden Logged in but not allowed A user editing someone else's data
404 Not Found No such resource Unknown URL or id
500 Internal Server Error The server crashed A bug or an outage

Headers describe the response: Content-Type (what the body is), Cache-Control (how long it may be cached), Location (where a new resource lives, or where to redirect), Set-Cookie and many more.

Node.js · editable

Why use a framework?

Our hand-written server already needs URL matching, JSON parsing, error handling and helper functions, and it would keep growing: cookies, file uploads, authentication, CORS… Express (next lesson) provides all of this, so a route becomes one line:

js
app.get('/products/:id', (req, res) => res.json(findProduct(req.params.id)));

Practice

  1. Add a DELETE /messages/:id route that removes a message and responds with 204.
  2. Make the server respond with 405 Method Not Allowed for PUT /messages.
  3. Add a GET /health route that returns { "status": "ok", "uptime": … } using process.uptime().
✎ Check your understanding

1.What must every request handler eventually do?

2.Which status code fits a successful POST that created a new item?

3.Why is the request body read with for await?

← Events and streams