Skip to content
Tutorials Center
My learning

Topics

Lesson 132 of 133 ยท Practice and Review

Practice Problems: Streams, HTTP and Concurrency

Fifteen graded Node.js problems on streams, HTTP servers and clients, and concurrency, each with a hint and a complete solution you can run and change in your browser.

The core problems trained your hands on modules, files, events, buffers and timers. This page raises the level. Fifteen problems cover the three areas where Node programs become real: streams that move data without loading it all, HTTP servers and clients, and concurrency that is controlled instead of accidental.

Every problem has a statement, a hint, and a complete solution that prints its results. Try each one yourself first, even if you only sketch the shape. When you compare, look at the parts that are easy to get wrong: where errors go, who closes what, and what happens when the data arrives in awkward pieces.

All HTTP examples run a server on 127.0.0.1 with port 0 (the operating system picks a free port), call it from the same program with fetch, and close the server at the end so the process exits. There is no network access to real hosts in our editor. Output that depends on timing is avoided: where order matters, the code forces it.

Part 1: streams

Problem 1: An uppercase Transform (easy)

Write a Transform stream that converts every chunk of text to upper case. Feed it from Readable.from(['hello ', 'streams']), and collect the result with pipeline.

Hint. In the transform(chunk, encoding, callback) method, call callback(null, chunk.toString().toUpperCase()). Use pipeline from node:stream/promises with a final async function that consumes the stream. See Transform Streams.

Node.js Solution 1: upper-case Transform

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

pipeline connects the stages, forwards errors from any of them and destroys all streams when one fails. Never use plain .pipe() for anything that matters.

Problem 2: Count lines across chunk boundaries (medium)

Network and file chunks are cut at arbitrary places, so a line can be split in two. Write a Transform that receives text chunks and emits one complete line per push. Test it with chunks that cut lines in the middle, and make sure a last line without a trailing newline is not lost.

Hint. Keep a leftover string. In transform, append the chunk, split by \n, keep the last piece as the new leftover and push the rest. Emit the leftover in flush. Set readableObjectMode: true so each line stays one item. See Stream Patterns.

Node.js Solution 2: line splitter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The blank line between beta and gamma survives as an empty string, which is usually what you want. The built-in readline module does the same job for you; building it once shows what it hides.

Problem 3: Respect backpressure (medium)

Write a slow Writable (each chunk takes a turn of the event loop to "save") with highWaterMark: 3 in object mode. Write ten numbers in a loop. Count how many times write() returned false, and wait for 'drain' each time so the buffer never grows beyond the limit. Print the saved numbers in order.

Hint. write() returns false when the internal buffer is at or above the high-water mark. When that happens, await once(writable, 'drain') before continuing. See Writable Streams and Backpressure and pipeline.

Node.js Solution 3: waiting for drain

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Without the await once(..., 'drain'), all ten numbers would pile up in memory. With ten numbers that is harmless; with a million lines from a fast file and a slow database it is a crash. This loop is exactly what pipeline does for you.

Problem 4: Batch with an async generator (medium)

Write an async generator function batch(source, size) that takes any async iterable and yields arrays of size items (the last one may be shorter). Use it inside pipeline between a source of numbers and a consumer that prints each batch.

Hint. Keep a current array. Push each item; when its length reaches size, yield it and start a new array. After the loop, yield the rest if it is not empty. A generator function can be a middle stage of pipeline. See Async Iterators.

Node.js Solution 4: batch generator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Batching is the standard trick for bulk inserts: one query for 500 rows is much cheaper than 500 queries. The generator is lazy, so memory use stays at one batch.

Problem 5: Gzip round trip (medium)

Compress a string with zlib.createGzip() using pipeline, collecting the compressed bytes. Then decompress them with createGunzip() and prove that the result equals the original. Also print whether the compressed data is smaller than the original for a highly repetitive text.

Hint. Write a small collect(streams) helper that runs pipeline with a final consumer that pushes chunks into an array and returns Buffer.concat(chunks). Do not print the compressed size itself: it can differ between zlib versions. See Compression with zlib.

Node.js Solution 5: gzip and gunzip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The two bytes 1f 8b open every gzip stream, so they make a handy sanity check when you inspect unknown data.

Part 2: HTTP

Problem 6: A JSON API with routing (medium)

Build a server with three routes: GET /items returns the list, POST /items adds an item from a JSON body and answers 201, anything else answers 404 with {"error":"not found"}. Call all three from the same program with fetch and print the status codes and bodies.

Hint. Read the body with for await (const chunk of request), concatenate and JSON.parse. Send JSON with response.writeHead(status, { 'content-type': 'application/json' }) and response.end(JSON.stringify(data)). See HTTP Servers and Building REST APIs.

Node.js Solution 6: small JSON API

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The try/catch turns a malformed body into a 400 instead of a crashed request. Notice that JSON.parse errors are the client's fault, so the status is in the 4xx range, not 500.

Problem 7: Limit the request body size (medium)

A server that reads whole bodies into memory can be crashed by one huge upload. Add a limit of 20 bytes: if the body is larger, stop reading and answer 413 Payload Too Large. Test with a small and a large body.

Hint. Count bytes while reading. When the total passes the limit, stop, respond with 413 and call request.destroy() only after the response has been sent (or just stop reading and let the response finish). See Request Bodies.

Node.js Solution 7: body size limit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The check happens while the data arrives, so the server never holds more than the limit plus one chunk. Real frameworks also check the content-length header first to reject obvious cases without reading anything.

Problem 8: Compose middleware (hard)

Write compose(middlewares) that returns a function run(context). Each middleware has the shape async (context, next) => {} and decides whether to call next(). Build three middlewares (a logger that records before and after, an auth check that stops the chain when context.user is missing, and a handler) and run the chain with and without a user.

Hint. Write an inner dispatch(index) that calls middlewares[index](context, () => dispatch(index + 1)) and returns a promise; return a resolved promise when the index is past the end. This is the core of Express and Koa. See Middleware.

Node.js Solution 8: middleware compose

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Code after await next() runs on the way back, so the logger can see the final status. A middleware that does not call next() ends the chain, which is how authentication and caching work.

Problem 9: Stream NDJSON from a server (hard)

Make a server that sends five JSON lines (newline-delimited JSON), one object per line, with a short pause between them. On the client, read response.body as it arrives, split it into lines and parse each one. Print the sum of the value fields.

Hint. On the server, use response.write(JSON.stringify(object) + '\n') and response.end(). On the client, for await (const chunk of response.body.pipeThrough(new TextDecoderStream())) gives strings; keep a leftover buffer as in problem 2. See Streaming Large Data and Web Streams.

Node.js Solution 9: NDJSON streaming

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The client never needs the whole body at once, and the number of chunks it sees does not matter, because the leftover logic reassembles lines no matter how the network cut them. The same code would handle a response of a million lines.

Problem 10: A request with a timeout (medium)

Start a server that answers after 200 ms. Call it with fetch and AbortSignal.timeout(30). Catch the error and print its name. Then call a fast endpoint with the same timeout and show that it succeeds. Do not print durations.

Hint. fetch(url, { signal: AbortSignal.timeout(30) }) rejects with a TimeoutError when the time is up. A manual AbortController rejects with AbortError instead, so check the name. See Abort Signals and The HTTP Client.

Node.js Solution 10: fetch with a timeout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

closeAllConnections() drops the connection that is still waiting for the slow handler, so the program can exit right away. In production you would give the timeout a value that matches your service, and pair it with a retry (problem 12).

Part 3: concurrency

Problem 11: A promise pool (hard)

Write pool(tasks, limit) that runs an array of async functions with at most limit running at the same time, and returns the results in the original order. Prove the limit works by tracking the highest number of tasks that were running at once.

Hint. Create limit workers. Each worker loops: take the next index from a shared counter, run that task, store the result at that index. The shared counter is safe because JavaScript runs one piece of code at a time. Start all workers and await Promise.all. See Concurrency Control.

Node.js Solution 11: concurrency-limited pool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The results array is in task order even though the tasks finish in a different order. Promise.all(tasks.map(run)) would start everything at once, which can overload a database or a remote API.

Problem 12: Retry with exponential backoff (medium)

Write retry(fn, { attempts, delay, sleep }) that calls fn, and on failure waits delay, then 2 * delay, then 4 * delay and so on before trying again. After the last attempt it throws the last error with the number of attempts as context. Inject sleep as a parameter so the example does not really wait, and record the delays it was asked for.

Hint. Loop over attempt numbers. In catch, if it is the last attempt throw new Error('failed after N attempts', { cause: error }); otherwise await sleep(delay * 2 ** (attempt - 1)). Injecting sleep is a common way to test timing code. See Error Handling in Production.

Node.js Solution 12: retry with backoff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Real systems add jitter (a random part in each delay) so a thousand clients do not retry at the same instant, and they only retry errors that can actually pass, such as timeouts and 503, never a 400.

Problem 13: Split work across worker threads (hard)

Sum the integers from 1 to 20,000,000 by giving each of two worker threads half of the range. Use one file: the main thread starts the workers with new Worker(import.meta.filename, { workerData }), and the worker branch computes and posts the result. Print the total.

Hint. Check isMainThread from node:worker_threads. Workers answer with parentPort.postMessage(sum). Wrap each worker in a promise that resolves on the 'message' event and rejects on 'error'. The formula n(n+1)/2 lets you check the answer. See Worker Threads.

Node.js Solution 13: two workers sum a range

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

On a machine with several cores the two halves run truly in parallel. Our sandbox has one CPU, so here the workers share it, but the structure is identical. The numbers stay below 2^53, so plain numbers are exact; for bigger sums you would use BigInt.

Problem 14: A promise-based mutex (hard)

Two async tasks read a shared counter, wait, and write the value plus one. Show that without protection the final value is wrong (a lost update). Then write a Mutex class with async run(fn) that lets only one critical section run at a time, and show that the final value is correct.

Hint. Keep a promise chain in #tail. In run(fn), create result = this.#tail.then(fn), then set this.#tail = result.catch(() => {}) so a failure does not block the next caller, and return result. See Concurrency Control and The Event Loop.

Node.js Solution 14: fixing a lost update

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node has no data races between threads in ordinary code, but logical races across await points are real: any time you read, wait and write, another task can sneak in between. Databases solve this with transactions and row locks; in memory a mutex or a queue does it.

Problem 15: Graceful shutdown (hard)

A server has one request in flight (a handler that takes a moment). Call server.close() while the request is running and show the order of events: the server stops accepting new connections, the running request still completes with its full response, and only then does the 'close' event fire. A new request after close() must fail.

Hint. server.close() stops listening but waits for existing connections. Turn off keep-alive on the response (connection: close) so idle sockets do not hold the server open. Collect events in an array and print it at the end. See Graceful Shutdown.

Node.js Solution 15: draining in-flight requests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A real shutdown handler also sets a deadline: after a few seconds it calls server.closeAllConnections() and exits anyway, so one stuck client cannot keep a deploy waiting forever.

How to review your own solutions

After each problem, ask four questions:

  • Who owns the error? Every await can reject. Is each failure turned into a result, a status code or a thrown error with a cause?
  • Who cleans up? Servers are closed, timers are cleared, streams are destroyed. A program that cannot exit is a bug.
  • What if the data arrives in pieces? Chunks, lines and bodies are cut at random places; test with awkward splits.
  • What is the limit? Body size, number of concurrent tasks, memory, time. Unlimited input is an attack.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A call to writable.write(chunk) returns false. What should the producer do?

    False means the internal buffer is full, not that the write failed; the chunk was accepted. Wait for 'drain' before writing more, otherwise memory grows without limit. pipeline() does this automatically.
  2. 2. Why does a line-splitting Transform keep a leftover string?

    Chunk boundaries have nothing to do with line boundaries. The text after the last newline may be the start of a line whose end arrives in the next chunk, so it is carried over, and emitted in flush() if the input ends without a newline.
  3. 3. Three tasks each read a shared value, await, then write value + 1. Starting at 0, what is a likely final value without any protection?

    All three read 0 before any of them writes, so each writes 1. This is a lost update across await points. A mutex, a queue or a database transaction makes the read-modify-write sequence run one at a time.
  4. 4. What does server.close() do to a request that is already being handled?

    close() stops accepting new connections and emits 'close' once all existing connections have ended. That is the basis of graceful shutdown; closeAllConnections() is the forceful option after a deadline.
  5. 5. Which error name does fetch reject with when AbortSignal.timeout(ms) expires?

    AbortSignal.timeout() aborts with a TimeoutError DOMException, while a manual controller.abort() without a reason gives an AbortError. Checking the name lets you tell a timeout from a user cancel.

Summary

  • Use pipeline to connect streams: it forwards errors and cleans up. Respect backpressure when you write by hand (write() returning false means wait for 'drain').
  • Chunks are cut anywhere: keep a leftover when parsing lines, and flush it at the end.
  • Async generators are easy middle stages for pipelines, and batch is a useful building block for bulk work.
  • HTTP servers need explicit status codes, body-size limits and error handling; fetch needs timeouts through AbortSignal.timeout.
  • Middleware is a chain of functions where code after await next() runs on the way back.
  • Limit concurrency with a pool, retry with backoff and a cause, and use workers for CPU-heavy parts.
  • A mutex or a transaction prevents lost updates across await, and server.close() plus a deadline gives a graceful shutdown.

Next: Node.js Interview Questions and Answers