Skip to content

Running Locally

The Course
•
15 min study

Move from the page to ordinary files

Download the reading-queue project. It contains the actual model, controller, service, UI, HTML, and automated tests. The in-page preview requires no installation; these steps are for learning how the same project runs outside the course.

  1. Install a current Node.js LTS release from the official Node site. Node executes JavaScript outside the browser. Its npm tool can run project commands.
  2. Extract the ZIP. Open a terminal in the extracted folder containing package.json. A terminal runs typed commands in its current folder; it is separate from a JavaScript console editor.
  3. Run node --version to verify installation. Run npm test to execute the included tests. No npm install is needed: the project uses only Node built-ins.
  4. Run npm start, leave that terminal open, and visit http://127.0.0.1:8080. Stop the local server with Ctrl+C. If the port is occupied, the included README explains changing it.

package.json declares type=module so Node interprets .js files as ES modules. Its scripts map npm test to Node's test runner and npm start to the included server. In "type": "module", type is a package.json configuration field, not an HTML attribute. Each command runs in the folder containing that file. The browser loads app.js as a module from index.html. The server serves only the listed local assets and is not a production hosting setup.

Do not paste terminal commands into a JavaScript editor or double-click the modular index.html. Relative browser modules need the HTTP-served origin. If a module fails, inspect its exact path and export name before changing its logic. Browser developer tools show Console errors and Network requests: open your browser's Developer Tools menu, select Console or Network, then reload. Paths in Network should refer to the local project.

The tests use node:test to define cases and node:assert/strict for assertions. Inspect one test and its inputs before running it. The runtime is a tool; understanding why a case protects a contract matters more than memorizing a command.

Predict and trace

Write the output before running. Follow the values through each call; if your prediction differs, identify the first step that surprised you.

Predict, then run JavaScript
const scripts = {
  test: "node --test project.test.js",
  start: "node server.mjs"
};

console.log(scripts.test);
console.log(scripts.start);
Check your prediction
Text
node --test project.test.js
node server.mjs

These are strings describing terminal commands, not commands executed by this console example.

Build it yourself

Exercise

Prove a test catches a broken rule

The project's npm test exists to catch a rule someone breaks by accident. Practise that here, with no install: below is the project's real decodeBooks from model.js. Write checkDecoder(decode) with one labelled check per rule: it trims id and title, rejects a blank title, rejects a duplicate id (after trimming), and rejects a value that is not a list. With the correct decoder it should print:

Text
4 checks passed

Then break the rule on purpose: delete the three lines starting if (ids.has(id)) and run again. Exactly one check should fail, by name. Put the lines back.

Your working space JavaScript
function decodeBooks(value) {
  if (!Array.isArray(value)) {
    throw new Error("Expected a list of books");
  }

  const ids = new Set();

  return value.map(item => {
    if (
      item === null ||
      typeof item !== "object" ||
      Array.isArray(item) ||
      typeof item.id !== "string" ||
      item.id.trim() === "" ||
      typeof item.title !== "string" ||
      item.title.trim() === ""
    ) {
      throw new Error("Each book needs an id and title");
    }

    const id = item.id.trim();

    if (ids.has(id)) {
      throw new Error("Duplicate book id");
    }

    ids.add(id);
    return { id, title: item.title.trim() };
  });
}

function checkDecoder(decode) {
  const failures = [];
  const check = (passed, label) => {
    if (!passed) failures.push(label);
  };
  // Add one labelled check per rule of decodeBooks.
  return failures;
}

const failures = checkDecoder(decodeBooks);
console.log(failures.length ? `FAILED ${failures.join(", ")}` : "4 checks passed");
Hint 1: the idea

A check for "rejects" has to call the decoder and report whether it threw, without letting the throw escape. The duplicate case only tests the rule if the two ids are equal after trimming but different before, otherwise a decoder that forgot to trim first could still pass.

Hint 2: a first step

Add a helper rejects(value) that returns true from a catch and false after a successful call. Use it for the non-list case first, rejects({ id: "js", title: "Notes" }), and run: you should see 4 checks passed even though only one check exists, which is a reminder to count your checks honestly before trusting that line.

Show one solution
JavaScript
function decodeBooks(value) {
  if (!Array.isArray(value)) {
    throw new Error("Expected a list of books");
  }

  const ids = new Set();

  return value.map(item => {
    if (
      item === null ||
      typeof item !== "object" ||
      Array.isArray(item) ||
      typeof item.id !== "string" ||
      item.id.trim() === "" ||
      typeof item.title !== "string" ||
      item.title.trim() === ""
    ) {
      throw new Error("Each book needs an id and title");
    }

    const id = item.id.trim();

    if (ids.has(id)) {
      throw new Error("Duplicate book id");
    }

    ids.add(id);
    return { id, title: item.title.trim() };
  });
}

function checkDecoder(decode) {
  const failures = [];
  const check = (passed, label) => {
    if (!passed) failures.push(label);
  };
  const rejects = value => {
    try {
      decode(value);
      return false;
    } catch {
      return true;
    }
  };

  const books = decode([{ id: " js ", title: " Notes " }]);
  check(books[0].id === "js" && books[0].title === "Notes", "trims id and title");
  check(rejects([{ id: "js", title: "" }]), "rejects a blank title");
  check(rejects([{ id: "js", title: "A" }, { id: " js", title: "B" }]), "rejects a duplicate id");
  check(rejects({ id: "js", title: "Notes" }), "rejects a non-list");
  return failures;
}

const failures = checkDecoder(decodeBooks);
console.log(failures.length ? `FAILED ${failures.join(", ")}` : "4 checks passed");
Text
4 checks passed

With the duplicate lines deleted, the program prints FAILED rejects a duplicate id, and only that check fails: each check targets one rule, so a failure names its cause. The duplicate fixture uses "js" and " js", which only collide once trimmed, so it also guards the order "trim, then compare". This is what npm test does in the downloaded project with node:test and assert, plus the four mutations that npm run test:javascript applies automatically in this course's own checks.

Explain before moving on

Which environment runs the UI and which runs the tests? What does package.json configure?

Going further

The last project test checks page-lifecycle wiring with a small DOM double. It uses Node's vm module to run app.js with controlled browser-like objects. That instrumentation is optional reading; you can understand the model and controller tests without learning vm. Browser keyboard and history-navigation checks are still needed for the real UI.

Read Node’s built-in test runner documentation after working through the examples. Use it as a reference when you meet an unfamiliar option; finishing every reference section is not required for this course.

Was this article helpful?

Your response is saved on this device.