How Node really works · Chapter 16 of 34

Reading line by line with readline

By the end of this chapter you can read any stream one line at a time, stop as soon as you have what you need, and ask questions in the terminal. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Events and streams
  • 5 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

The kitchen printer keeps a log, one line per event, and Ravi wants to know each morning how many errors there were and what they said. The counter staff want a tiny command line tool that asks for a name and a cup count when the app is down. And Kabir wants the best sellers from an order log far too big to load.

In chapters 13 and 14 you rebuilt lines from chunks by hand: a leftover, a split, a flush at the end, care with \r. Doing that in every script is slow to write and easy to get wrong, and none of it helps you ask a question and wait for the answer. node:readline does both.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    readLines: read the kitchen log line by line, Windows line endings and all

    Write readLines(path). It reads the file at path with readline and returns a promise of all its lines, in order, without '\n' or '\r\n'. An empty line in the middle is kept as ''. A last line without a line break still counts, and a line break at the very end adds no empty line.

  2. Task 2

    summariseLog: count the kitchen log by level

    Write summariseLog(path). Each line of the log looks like '16:02 ERROR paper jam': a time, a level, then the message, separated by spaces. Lines may end in '\r\n', and blank lines are ignored.

    It returns a promise of { total, levels, errors }: total is the number of non-blank lines, levels has a count for each level seen (only levels that appear), and errors is the messages of the ERROR lines, in order, without the time or level ('paper jam').

  3. Task 3

    findFirst: stop reading as soon as the first match turns up

    Write findFirst(input, word). input is a readable stream of text. It returns a promise of the first line that contains word, without its line ending, or null if no line does.

    Stop reading as soon as you find it: the check counts how much of a 50,000 line stream was pulled. Destroy input when you are done, whether a line was found or not.

  4. Task 4

    askOrder: ask the counter for a name and a cup count

    Write askOrder(input, output). Use readline/promises on the streams you are given (never the process's own stdin or stdout) to ask 'Name? ' and then 'How many cups? '. Keep asking 'How many cups? ' until the answer is a whole number of at least 1: 'two', '0' and '1.5' are all asked again.

    Write nothing to output but the prompts themselves, exactly as above (no error message between them). Close the interface, then resolve with { name, cups }: the name with spaces trimmed from both ends, and cups as a number.

  5. Task 5 · interview question

    Interview: find the best sellers in a huge order log

    Write topItems(path, n). The file is a CSV order log with the header 'id,item,qty,price' and may hold blank lines, which are ignored. It returns a promise of the n items that sold the most cups (the sum of qty for that item), as [{ item, cups }], most cups first; items with the same cups go in A to Z order of their names. When there are fewer than n items, return them all.

    Stream the file with readline and keep only one running total per item, never the lines themselves.

What you will learn

  • createInterface: any readable stream in, one line at a time out, with for await.
  • Line endings: \r\n from Windows files, and what crlfDelay really does.
  • Stopping early: leaving the loop, and closing what readline leaves open.
  • question: asking on any pair of streams, then closing.

More in Events and streams

Build "Reading line by line with readline" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.