Sikau

Project: An orders API · Day 1 · about 35 min

A server from scratch

node:http, routing by method and path, JSON responses and status codes.

One API, from raw Node to NestJS

This combined project brings Node.js and NestJS together. You'll build the café's orders backend three ways — raw node:http, then Express, then a structured NestJS app — so you see exactly what each layer does for you.

  • Day 1: a server with only node:http
  • Day 2: Express with middleware, pagination and error handling
  • Day 3: a NestJS service with business rules
  • Day 4: a guard for staff and kitchen
  • Day 5: the finished orders API

Requests in, JSON out

import http from "node:http";
import { MENU } from "./lib/menu.js";

const server = http.createServer((req, res) => {
  const url = new URL(req.url, "http://localhost");
  const send = (status, body) => {
    res.writeHead(status, { "Content-Type": "application/json" });
    res.end(JSON.stringify(body));
  };

  if (req.method === "GET" && url.pathname === "/health") return send(200, { ok: true });
  if (req.method === "GET" && url.pathname === "/menu") {
    const vegOnly = url.searchParams.get("veg") === "true";
    return send(200, vegOnly ? MENU.filter((d) => d.veg) : MENU);
  }
  send(404, { error: "Not found" });
});

server.listen(3000, () => console.log("Listening on http://localhost:3000"));

The rest of this lesson

  • What a request really isexplain
  • Status codes clients rely onexplain
  • What does the client get?predict
  • Pick the statusmatch
  • Write the serverwrite code
  • Badge quizquiz
  • What you builtrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 5 lessons of Project: An orders API