In-Class NodeJS Exercise 2

Exercise · Explain This Node.js Server

Node.js · Backend Basics
Explain this code out loud. Focus on HTTP servers, requests, responses, routes, and status codes.
const http = require("http");

const server = http.createServer((req, res) => {
    if (req.url === "/") {
        res.statusCode = 200;
        res.end("Welcome to the homepage");
    } else if (req.url === "/about") {
        res.statusCode = 200;
        res.end("This is the about page");
    } else {
        res.statusCode = 404;
        res.end("Page not found");
    }
});

server.listen(3000, () => {
    console.log("Server is running on http://localhost:3000");
});
🎯 Instructions oral task
  1. Explain what the script creates.
  2. Explain what the http module does.
  3. Describe what req represents.
  4. Describe what res represents.
  5. Explain the route logic.
  6. Explain the difference between status code 200 and 404.
  7. Explain what listen(3000) does.
📖 Vocabulary core
  • server — a program that listens for requests and sends responses.
  • request — data sent from the client to the server.
  • response — data sent from the server back to the client.
  • route — a URL path that the server responds to.
  • status code — a number that describes the result of a request.
  • port — a communication channel used by the server.
🧩 Collocations natural English
  • create a server
  • handle a request
  • send a response
  • check the URL
  • return a 404 error
  • listen on a port
🗣️ Phrasal Verbs interview speech
  • set up — "This code sets up a basic HTTP server."
  • come in — "When a request comes in, the callback runs."
  • send back — "The server sends back a response."
  • fall back to — "If no route matches, it falls back to a 404 response."
🎤 Model Answer spoken

This script creates a basic Node.js HTTP server.

First, it imports the built-in http module. This module allows us to create a web server without using Express.

The createServer() method takes a callback function with two parameters: req and res. The req object represents the incoming request, and the res object represents the response we send back.

The code checks the URL. If the user visits /, the server sends the homepage message. If the user visits /about, it sends the about page message.

If the URL does not match either route, the server returns a 404 status code and sends “Page not found.”

Finally, server.listen(3000) starts the server on port 3000.