Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

http-server-basics

HTTP server basics

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches HTTP server basics: the syntax, APIs, and habits you need before advancing in Node.js.

HTTP routing and middleware appear in every Node API interview—know request/response objects and status codes.

You will apply HTTP server basics in contexts like: REST APIs, webhooks, and BFF layers behind React or mobile clients.

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

When you can explain the previous lesson's ideas without copying starter code.

Node ships node:http to create HTTP servers without third-party frameworks. Every Express app ultimately sits on the same primitives—understanding them clarifies routing, headers, and status codes.

Minimal server

import http from 'node:http';

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

server.listen(3000);

Request object

req.method, req.url, and headers describe the incoming request. You must read the body from req streams for POST data—frameworks parse this for you.

Playground pattern

The runner uses a short-lived server: listen on port 0 (OS picks a free port), log, then close() and process.exit(0) so the sandbox finishes cleanly.

Important interview questions and answers

  1. Q: What does res.writeHead do?
    A: Sends status code and response headers before the body—must happen before res.end for that response.
  2. Q: HTTP vs HTTPS in Node?
    A: node:https module with TLS certificates—often terminated at a load balancer in production.

Self-check

  1. Which module creates a bare HTTP server?
  2. Why close the server in playground demos?

Challenge

Hello HTTP

  1. Run the default node:http server code.
  2. Confirm the terminal logs a port and "Server closed".
  3. Change the response body to include your name.

Done when: output shows your custom greeting and a clean shutdown.

Interview prep

createServer vs Express?

http.createServer is low-level—you parse URLs and bodies yourself; Express adds routing, middleware, and helpers on top of the same HTTP module.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • createServer flow?
  • Why close server in lesson?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump