Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NODENode.js

    Runtime and core tooling

    • What is Node.js?
    • CommonJS and ES modules
    • npm and package.json
    • process and command-line apps

    Async I/O and events

    • Event loop: callbacks to async/await
    • Files and paths with fs/promises
    • EventEmitter
    • Streams and backpressure

    HTTP and APIs

    • Your first HTTP server
    • Routing and JSON bodies
    • Build a tiny router and middleware chain
    • REST: resources and status codes
    • Validation and error handling

    Production: security, testing and deployment

    • Authentication basics with crypto
    • Configuration and environment secrets
    • Testing with assert and node:test
    • API security fundamentals
    • Project: tasks REST API and deployment
    Node.js

    HTTP and APIs · Lesson 9 of 18

    Your first HTTP server

    Receive a request and send status, headers and a body with node:http.

    12 minutes2 games

    node:http is the foundation frameworks build on. createServer receives a handler for every request: req contains method, URL and headers, while res sets status, headers and closes the response with end. A server stays alive until closed.

    Client request

    HTTP server handler

    Business logic

    Status + headers + body

    Node.js
    const http = require("node:http")
    const server = http.createServer((req, res) => {
      res.writeHead(200, { "content-type": "text/plain; charset=utf-8" })
      res.end("hello node")
    })
    server.listen(0, async () => {
      const port = server.address().port
      const response = await fetch("http://127.0.0.1:" + port + "/")
      console.log(response.status, await response.text())
      server.close()
    })

    listen(0) lets the OS choose an available port, which is ideal in tests. Production usually reads process.env.PORT. Every path must end the response exactly once; real services also handle client disconnects and timeouts.

    Important

    Do not write a body or internal error before appropriate headers. Once headersSent is true, status cannot be changed. Set a content type and never expose stack traces to clients.

    Quick question

    What closes and completes a response?

    Interview question

    Why can Node handle many HTTP connections?

    Exercise

    Create a server returning SeaCode API, fetch it, print 200 SeaCode API, then close it.

    Expected output: 200 SeaCode API

    Node.js
    const http = require("node:http")
    const server = http.createServer((req, res) => {
    
    })
    server.listen(0, async () => {
      const response = await fetch("http://127.0.0.1:" + server.address().port)
      console.log(response.status, await response.text())
      server.close()
    })

    Learn more from

    • Node.js: HTTPnodejs.org
    • MDN: HTTP overviewdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousStreams and backpressureNextRouting and JSON bodies
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.