Menu
CoddyTech

The http Module

Part of the Express Basics section of Coddy's Node.js journey. Lesson 2 of 20.

Express does work for you that you cannot see. Node's own node:http module, which Express is built on, shows it.

import http from 'node:http';

const server = http.createServer((req, res) => {
    if (req.url === '/repairs') {
        res.statusCode = 503;
        res.end('Repairs are closed today');
        return;
    }
    res.end('Welcome to the bike shop');
});

server.listen(3000);

Node calls the function for every request. There are no routes: you compare req.url, the path that was asked for, yourself. res.statusCode is the status code, a number that says how it went: 200 means OK, 404 means not found. res.end sends the body and finishes the response, so set the status before it.

challenge icon

Challenge

Easy
  • / answers Home.
  • Every other path answers status 404 with Not found.

Press Run, then send GET / and GET /nope. The next lesson does the same with Express.

Try it yourself

import http from 'node:http';

const server = http.createServer((req, res) => {
    // Answer / with Home, and every other path with 404 Not found.
    res.end('Hello from Node');
});

server.listen(3000, () => {
    console.log('Listening on port 3000');
});
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Express Basics