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 13 of 18

    Validation and error handling

    Separate client mistakes from server faults and return a stable error shape.

    12 minutes2 games

    Input arrives from untrusted boundaries: body, query, params and headers. Validate at the boundary, producing known typed data or a 4xx response. Unexpected faults go to a central error handler that logs safe context and returns a generic 500.

    Raw request

    Parse

    Validate

    Business logic

    Central error mapper

    Node.js
    function validateTask(body) {
      if (!body || typeof body.title !== "string") return { ok: false, status: 400, error: "title is required" }
      const title = body.title.trim()
      if (title.length < 2) return { ok: false, status: 422, error: "title is too short" }
      return { ok: true, value: { title } }
    }
    console.log(validateTask({ title: "  Learn Node  " }))
    console.log(validateTask({ title: "x" }).status)

    Use custom errors with status and code for expected cases such as NOT_FOUND, while programming errors remain 500. Keep one response shape such as { error: { code, message, details } }. Log a correlation or request ID rather than sensitive bodies.

    Important

    Do not trust TypeScript types at a network boundary; types disappear at runtime. Never return every exception message blindly because it can expose SQL, paths or secrets. Map known errors through an allowlist.

    Quick question

    The database connection unexpectedly fails. Return?

    Interview question

    Where should validation live: controller or database?

    Exercise

    Write validateAge(value) returning ok for an integer from 18 to 120, otherwise invalid. Print ok invalid invalid.

    Expected output: ok invalid invalid

    Node.js
    function validateAge(value) {
    
    }
    console.log(validateAge(25), validateAge(17), validateAge("25"))

    Learn more from

    • OWASP: Input Validationcheatsheetseries.owasp.org
    • Node.js: Error handling practicesnodejs.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousREST: resources and status codesNextAuthentication basics with crypto
    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.