Up Next: Professional Node

See what your APIs can do after Module 5, from live updates over WebSockets to streamed files, uploads with limits and work that runs after the response, meet the Live Quiz Night project where you'll show it, and get a first look at Module 6, where your API is logged, checked on every push and put online.

Journeyman · Node.js · Web Development · Updated

By

After this lesson you can

  • Choose the Module 5 tool that fits a job, from WebSockets and broadcasting to streams, upload limits, a 202 with a job queue and worker threads

Key takeaways

  • After Module 5 a server can speak first, over WebSockets, to one client or to everyone at once, and handle big files, uploads and slow work without making anyone wait.
  • Live Quiz Night is a quiz your friends play live from their phones, with a ws server that sends every question at the same moment and the scoreboard after each one.
  • Each Module 5 job has its own tool. WebSockets push, streams move big data, multer's limits guard uploads, 202 and a queue move slow work, and worker threads take heavy work.
  • Module 6 is how professionals ship. Structured logs, settings checked at startup, a clean shutdown, tests on every push, and your API online at a link.

At the start of this module, your APIs could only answer. Now the server can speak first, the moment something happens, and it can take a 2 GB file, a stranger's upload or a slow job in its stride. That's the difference between an API that works in a demo and one that holds up when real people use it at once.

This lesson looks back at what you can do, shows you the project where you'll put it together, and gives you a first look at Module 6, where your API gets ready for the world.

What you can do now

After Module 5, you can:

  • push live updates with a ws server beside Express: messages as JSON with a type, sent the moment a goal is scored instead of when someone asks;
  • broadcast to every open client, keep groups (rooms) in a Map, clean up when a client leaves, and test it all with real WebSocket clients under node --test;
  • stream big files with pipeline, read a big CSV line by line with readline, and send a CSV download that never sits in memory whole;
  • accept uploads with multer, under a name the server makes, with limits that answer 413 and 400;
  • answer 202 Accepted for slow work, run it after the response on a job queue, and give heavy calculations a worker thread.

Show what you can do: Live Quiz Night

Quiz night with friends usually means one person reading questions off a phone and someone else keeping score on a napkin. Module 5's project is the app for it: Live Quiz Night.

  • Players join from their phones with a name; the host's page and the players' page come ready-made, and you write the server with ws.
  • The host starts a round, and every player gets the question at the same moment.
  • Answers are scored by speed, and the scoreboard is broadcast to everyone after each question.
  • A player who drops out is removed, so the scoreboard never waits for someone who left.

Every message is JSON with a type, and the tests connect real WebSocket clients under node --test.

Which tool fits?

Most of the project, and most real features, come down to one question: which job is this, and which tool does it?

The job The tool
Tell every player the next question the moment the host starts it A ws server that broadcasts to every open client
Keep each quiz table's players together Rooms: a Map from the room's name to a Set of clients
Let the host download every answer of the night A CSV sent with pipeline and res.attachment
Read a 2 GB log file to count errors readline over a createReadStream, one line at a time
Let players upload a team photo, safely multer with limits.fileSize, a fileFilter and a name the server makes
Email everyone their results without making them wait Answer 202 Accepted, then a job on the queue
Crunch a year of scores into statistics A worker thread, so the event loop stays free

Try it. For each message Live Quiz Night sends, write down whether it goes to one player, one room or everyone.

A sneak peek: what a server writes down

So far your servers have spoken to you with console.log. Online, nobody is watching the terminal, and the log is how you find out what happened at 3 a.m. Compare two ways of writing the same request down:

const request = { method: "POST", url: "/entries", status: 202, ms: 41 };

console.log(`${request.method} ${request.url} answered ${request.status} in ${request.ms} ms`);
console.log(JSON.stringify({ level: "info", msg: "request done", ...request }));
POST /entries answered 202 in 41 ms
{"level":"info","msg":"request done","method":"POST","url":"/entries","status":202,"ms":41}

A person reads the first line at a glance. A program can search the second: "every request slower than 500 ms", "every 413 today". (...request, read "spread request", copies each of its properties into the new object.) Lines like the second are structured logs, and Module 6 starts with them.

What Module 6 unlocks

Module 6, Professional Node, is how professionals ship:

  • Logs, configuration and shutdown: structured logs with pino, every setting checked when the server starts, a clean shutdown, a /health route, and performance you measure before you change anything.
  • Code quality and CI: ESLint and Prettier, and GitHub Actions running your tests on every push, with a green badge in your README.
  • An API online: a free MongoDB Atlas cluster and Render's free web service, so your API has a link you can share.

Its project is the Neighborhood Book Swap API: a small API where neighbors list books to give away and claim one, tested on every push and online at a link.

Check yourself

2 questions to try on the lesson page.

So what

Your servers can now push, stream, take uploads and work in the background; Live Quiz Night is your chance to prove it, and Module 6 makes your work something you can put online and show anyone.

Key terms

Related