Up Next: Real-Time Apps
See what your APIs can do after Module 4, from hashed passwords and tokens to rules about who may do what and an API hardened for the open internet, meet the Club Events API project where you'll show it, and get a first look at Module 5, where the server speaks first.
Journeyman · Node.js · Web Development · Updated
After this lesson you can
- Choose the Module 4 tool that fits a security job, from password hashing and tokens to CORS, helmet, rate limits and allow-lists
Key takeaways
- After Module 4 an API can store passwords safely, sign members in with tokens, check who may do what, and stand up to the visitors it meets online.
- The Club Events API is a hiking club's events API, where members sign up for hikes and only organizers create them, with a test for each role.
- Each security job has its own tool. bcryptjs hashes passwords, a JWT says who's asking, middleware checks roles, and cors, helmet, rate limits and allow-lists guard the doors.
- Module 5 makes the server speak first, with WebSockets, and handles big files, uploads and slow work without making anyone wait.
At the start of this module, anyone could call your APIs and do anything. Now they have members: people who sign up, sign in, and may do only what their role allows, behind an API that gives strangers nothing to work with. That's the difference between a project that runs on your laptop and one you can put in front of real people.
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 5, where the server stops waiting to be asked.
What you can do now
After Module 4, you can:
- store passwords safely as bcryptjs hashes, never the passwords themselves;
- sign members up and in, refusing a used email with 409 and a wrong email or password with one vague 401;
- keep members signed in with session cookies or with tokens (JWT) in the
Authorizationheader, and say when each fits; - require a signed-in user with middleware, and tell 401 ("who are you?") from 403 ("I know who you are, and you can't");
- give users roles and check that people change only their own things;
- harden an API with CORS for its own pages, helmet's headers, a rate limit on sign-in, secrets in
.env, text checks against operator injection and allow-lists against mass assignment.
Show what you can do: the Club Events API
Switchback Hiking Club plans a hike most weekends, and its sign-up sheet is a group chat that nobody can scroll back through. Module 4's project is the club's API: the Club Events API.
- Members sign up and sign in, with passwords hashed by bcryptjs, a unique index on email, and a JWT in the
Authorizationheader. - Members sign up for hikes, and cancel only their own sign-ups.
- Organizers create and edit hikes; anyone else who tries gets 403.
- It's ready for the open internet: CORS for the club's web page, helmet, a rate limit on sign-in, and every secret from environment variables.
Supertest tests with mongodb-memory-server sign in as a member, as an organizer and as nobody at all, and check every rule for each.
Which tool fits?
Most of the project comes down to one question for each route: which job is this, and which tool does it?
| The job | The tool |
|---|---|
| Store a password so a stolen database doesn't give it away | bcrypt.hash, and bcrypt.compare at sign-in |
| Say who's asking, on every request | A JWT from jwt.sign, sent as Authorization: Bearer <token> and checked with jwt.verify |
| Turn away a request with no valid token | A requireUser middleware answering 401 |
| Let only organizers create a hike | A requireRole("organizer") middleware answering 403 |
| Let only the club's web page call the API from a browser | cors with an allow-list of origins |
Send safe headers and hide X-Powered-By |
helmet() |
| Slow down password guessing | A signInLimiter from express-rate-limit on sign-in, answering 429 |
| Keep the JWT secret out of the repository | process.env, a .env file in .gitignore, and .env.example |
| Stop a body from making someone an organizer | An allow-list of the fields a request may change |
Try it. For each route of the Club Events API, write down the jobs from the table it needs. Sign-in alone needs four.
A sneak peek: when the server speaks first
Every API so far waits to be asked. That's a problem for a live scoreboard, the first example in Module 5: a school's intramural soccer game, with parents following the score on their phones. If each phone asks for the score every 2 seconds, here's the bill for one 50-minute game:
const viewers = 200;
const minutes = 50;
const askEverySeconds = 2;
const requests = (viewers * minutes * 60) / askEverySeconds;
const goals = 4;
console.log(`Requests: ${requests}`);
console.log(`Answers with news: ${viewers * goals}`);
Requests: 300000
Answers with news: 800
Asking again and again is called polling, and here almost every answer is "nothing new". What the scoreboard needs is for the server to tell every phone the moment a goal is scored, and that's what Module 5 starts with.
What Module 5 unlocks
Module 5, Real-Time Apps, makes the server speak first:
- WebSockets: a connection that stays open like a phone line, so the server can send the score the moment it changes, to every viewer at once.
- Streams, uploads and background work: files too big to hold in memory, sent a piece at a time; photo uploads with size and type limits; and slow work that finishes after the answer has gone.
Its project is Live Quiz Night: a quiz game your friends play live from their phones, where everyone gets each question at the same moment and the scoreboard updates after every round.
Check yourself
2 questions to try on the lesson page.
So what
You can now build an API that knows its members, keeps their passwords safe and turns strangers away, and the Club Events API is your chance to prove it before Module 5 makes it live.