Capstone projects: three programs, built step by step

The last part of the course is three programs rather than three topics. Each one is built over several lessons, and each lesson ends with a program that runs: a task tracker that keeps its list in a JSON file, an inverted index that finds the documents that hold a word, and the index served over HTTP and shipped as a command. The pieces are the ones the earlier parts taught, put in the order a project needs them.

A project is not a bigger example. It has files that each own one thing, tests that run without a server, a manifest and a linter, and a command a reader can type. The lessons add one file at a time and change what they must, so a reader who stops after any lesson has a program that works.

The three projects

Project A starts in lesson 124 and is a command line tool: a task has an id, a title and a done flag, the list lives in a JSON file, and the commands are add, list, done and remove. It ends with tests and a help text.

Project B starts in lesson 130 and is a library: text becomes words, the words become a Map from a term to the ids of the documents that hold it, and a query returns the documents in the order of their score. It has no command line of its own.

Project C starts in lesson 134 and puts project B behind an HTTP endpoint. It is a package with a bin entry, so the endpoint starts with one word instead of a path.

The table is the whole part at a glance. The lessons are counted the way the course counts them, and the three ranges do not overlap.

What each lesson adds

The program below is the plan as data: an array of three objects, one per project, with the range of lessons, a line about what the project does and the files it ends with. It prints a block per project and one line with the totals, which is the shape of every program in this part: build a value, then print it.

The file plan.js:

vi plan.js

const projects = [ { name: 'tasks', lessons: '123 to 129', summary: 'a command-line task tracker over a JSON file', files: ['bin/tasks.js', 'src/task.js', 'src/storage.js', 'src/commands.js'], }, { name: 'search', lessons: '130 to 133', summary: 'an inverted index over three documents', files: ['src/tokenizer.js', 'src/index.js', 'src/query.js', 'src/search.js'], }, { name: 'search-http', lessons: '134 to 136', summary: 'the index behind an HTTP endpoint, shipped as a command', files: ['server.js', 'src/handler.js', 'bin/search-serve.js', 'package.json'], }, ]; for (const project of projects) { console.log(project.name + ' (' + project.lessons + '): ' + project.summary); for (const file of project.files) { console.log(' ' + file); } } const files = projects.reduce((total, project) => total + project.files.length, 0); console.log('projects: ' + projects.length + ', files: ' + files);

node plan.js

tasks (123 to 129): a command-line task tracker over a JSON file
  bin/tasks.js
  src/task.js
  src/storage.js
  src/commands.js
search (130 to 133): an inverted index over three documents
  src/tokenizer.js
  src/index.js
  src/query.js
  src/search.js
search-http (134 to 136): the index behind an HTTP endpoint, shipped as a command
  server.js
  src/handler.js
  bin/search-serve.js
  package.json
projects: 3, files: 12

Twelve files, four per project. The order inside a block is the order the lessons add them, and the reduce counts the entries of the three arrays without a second loop.

Project A adds a file in lessons 124, 125, 126 and 127: the model, the storage, the commands and the tests. Lesson 128 changes two of them and lesson 129 reads the whole tracker without adding a line.

Project B adds three files and one facade: the tokenizer in 130, the index in 131, the query in 132, and the module that hides the three behind one function in 133.

Project C adds the server in 134, moves the handler into a module and adds the command in 135, and lesson 136 runs the finished package and looks at what comes after the course.

The folder layout

A project is a folder. The lessons of project A work in a folder named tasks, project B in search, and project C in search-http, which starts as a copy of the folder of project B. The three folders sit beside each other, and the layout of each one is below.

The folders of the three projects:

tasks/ bin/tasks.js the command line src/task.js one task and its errors src/storage.js the JSON file src/commands.js the four commands test/task.test.js test/storage.test.js package.json eslint.config.js search/ src/tokenizer.js text to words src/index.js words to document ids src/query.js ids to ranked results src/search.js the three behind one function test/tokenizer.test.js test/search.test.js package.json eslint.config.js search-http/ bin/search-serve.js the command of the package server.js a server that probes itself src/ copied from search/ test/ copied from search/ package.json eslint.config.js

The src folder holds the code that another file requires, and the top level holds what a reader types: the command of project A and the probing server of project C. A test file never sits beside the code it tests, so the linter and the test runner can treat test as one thing.

The lessons write these files into a scratch folder under /tmp, and so does every program that needs a file at all: a temporary folder made with mkdtemp is removed before the program ends, and no path of the machine appears in the output of a lesson. The lesson on temporary folders shows the pattern.

How to run a step

Four commands cover every lesson of the part. node --check parses a file without running it, which catches a broken brace before a test does; node runs a program; node --test runs the test files of the folder; and npx runs the linter of the project.

node --check src/task.js
node check.js
node --test --test-reporter=dot
npx eslint .

The dot reporter prints one character per test and nothing else, which is why the outputs of this part are short and the same on every machine: a duration and a file path would change with every run. A failing test prints a cross instead of a dot, and the runner then prints the assertion that failed.

Each lesson starts by naming the state of the folder, so the reader knows which files exist before the lesson adds one. The commands of a lesson assume the current folder is the project folder, and no lesson needs a package installed for anything except the linter.

Exercises

  1. Add a fourth project to the array with two files and run the program. Which two numbers of the last line change, and why does the list keep its order?
  2. Remove the files array from the last project and run the program again. Which line fails, and what does Node.js print?
  3. Print the names of the projects joined by a comma instead of one block per project. Which method replaces the two loops?
  4. Which project has a package.json from its first lesson, and which one gets the interesting one only in lesson 135?
  5. Run node --check on a file with a missing closing brace. Which line and column does the report name, and what is the exit status?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which three programs does the last part build?

    Answer

    A task tracker, a search index and the index behind an HTTP endpoint

    Each project uses the pieces of the earlier parts and adds one layer on top.

  2. What does this program print? const projects = ["tasks", "search"]; console.log(projects.length, projects.join(" then "));

    Answer

    2 tasks then search

    The array has two entries and the join puts the word between them.

  3. Why does every lesson of the part end with a program that runs?

    Answer

    A project is read as working code, and the output is the proof that it works

    The course shows the real output of the real file, so a reader can compare a run with the page.

  4. What does this program print? const files = [["a.js"], ["b.js", "c.js"]]; console.log(files.reduce((total, list) => total + list.length, 0));

    Answer

    3

    The reducer starts at zero and adds the length of each list: one and two.

  5. Which command checks a file without running it?

    Answer

    node --check

    The parser reads the file and reports a syntax error with its position, and nothing runs.

  6. What does this program print? const project = { name: "tasks", lessons: "123 to 129" }; console.log(project.name + " " + project.lessons);

    Answer

    tasks 123 to 129

    The two fields are read from the object and joined with a space.

  7. Why is the test file of a project kept in its own folder?

    Answer

    So the code that runs and the code that checks it are separate, and a runner can find one folder

    A test is not part of the program, and one folder for the tests keeps the source folder about the program.

  8. What does this program print? const steps = ["model", "storage", "commands"]; console.log(steps.indexOf("storage"));

    Answer

    1

    The index is counted from zero, so the second entry is at one.

  9. Which two files does a project folder of this part hold at its top level from the start?

    Answer

    package.json and eslint.config.js

    The manifest names the scripts and the config tells the linter what the code is.

  10. What does this program print? const project = { files: ["a.js", "b.js"] }; console.log(project.files.includes("c.js"), project.files.length);

    Answer

    false 2

    The array has two entries and no c.js among them.

The programs and commands on this page were run with Node.js 24.22.1 and npm 11.12.1, and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev