Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
User registration flow
📖 Scenario: You are building a simple user registration flow for a website using Express.js. Users will send their username and password to register.
🎯 Goal: Create an Express.js server that accepts user registration data, validates it, and stores it in memory.
📋 What You'll Learn
Create an Express app instance
Set up a route to accept POST requests at /register
Store registered users in an in-memory array
Validate that username and password are provided
Send appropriate success or error responses
💡 Why This Matters
🌍 Real World
User registration is a common feature in web apps to create accounts and manage access.
💼 Career
Understanding how to build backend routes for user registration is essential for web developers working with Node.js and Express.
Progress0 / 4 steps
1
Set up Express app and users array
Create an Express app by requiring express and calling express(). Then create an empty array called users to store registered users.
Express
Hint
Use require('express') to import Express and call it to create the app. Then declare users as an empty array.
2
Add JSON body parsing middleware
Add middleware to the Express app to parse incoming JSON request bodies by calling app.use(express.json()).
Express
Hint
Use app.use(express.json()) to enable JSON body parsing for POST requests.
3
Create POST /register route with validation
Create a POST route at /register using app.post. Inside the route handler, extract username and password from req.body. If either is missing, respond with status 400 and JSON { error: 'Username and password required' }. Otherwise, add an object with username and password to the users array and respond with status 201 and JSON { message: 'User registered' }.
Express
Hint
Use app.post('/register', (req, res) => { ... }). Extract username and password from req.body. Check if either is missing and respond with 400 and error JSON. Otherwise, add the user to users and respond with 201 and success JSON.
4
Start the server listening on port 3000
Add code to start the Express app listening on port 3000 by calling app.listen(3000). Add a callback that logs 'Server running on port 3000'.
Express
Hint
Use app.listen(3000, () => { console.log('Server running on port 3000'); }) to start the server.
Practice
(1/5)
1. What is the main purpose of using app.post('/register', ...) in an Express user registration flow?
easy
A. To update user details after registration
B. To serve the registration form HTML page
C. To handle incoming registration data sent by the client
D. To delete a user from the database
Solution
Step 1: Understand HTTP methods in Express
app.post is used to handle POST requests, which usually send data to the server.
Step 2: Identify the role of the '/register' route
The '/register' route is commonly used to receive new user data for account creation.
Final Answer:
To handle incoming registration data sent by the client -> Option C
Quick Check:
POST route = handle registration data [OK]
Hint: POST routes handle data sent from forms or clients [OK]
Common Mistakes:
Confusing GET with POST for data submission
Thinking app.post serves HTML pages
Mixing update/delete operations with registration
2. Which of the following is the correct way to parse JSON data sent in a POST request in Express?
easy
A. app.use(express.urlencoded())
B. app.use(express.json())
C. app.get(express.json())
D. app.post(express.text())
Solution
Step 1: Identify middleware for JSON parsing
Express provides express.json() middleware to parse JSON request bodies.
Step 2: Confirm correct usage
It should be used with app.use() to apply globally or on specific routes.
Final Answer:
app.use(express.json()) -> Option B
Quick Check:
Use express.json() middleware for JSON data [OK]
Hint: Use express.json() to parse JSON POST data [OK]
Common Mistakes:
Using express.urlencoded() for JSON data
Calling express.json() inside app.get()
Using express.text() for JSON parsing
3. Given the following Express route code for user registration, what will be the response if the user already exists?
A. Missing return after sending 400 response causes multiple responses
B. Incorrect HTTP method used; should be GET instead of POST
C. users.push should be outside the route handler
D. No error; code works correctly
Solution
Step 1: Analyze the if condition handling missing fields
The code sends a 400 response if username or password is missing but does not stop execution.
Step 2: Identify consequence of missing return
Without return, the code continues and tries to push user and send another response, causing an error.
Final Answer:
Missing return after sending 400 response causes multiple responses -> Option A
Quick Check:
Return after res.send to stop execution [OK]
Hint: Always return after sending response to avoid errors [OK]
Common Mistakes:
Not returning after res.send in condition
Using GET instead of POST for data submission
Trying to push users outside route handler
5. You want to improve the user registration flow to prevent duplicate usernames and ensure passwords are at least 8 characters. Which code snippet correctly implements these checks before saving the user?
app.post('/register', (req, res) => {
const { username, password } = req.body;
// Your code here
});
hard
A. if (users.includes(username)) { res.status(400).send('User exists'); return; }
if (password.length < 8) { res.status(400).send('Password too short'); return; }
users.push({ username, password });
res.status(201).send('User registered');
B. if (!users.includes(username)) { res.status(400).send('User exists'); return; }
if (password.length > 8) { res.status(400).send('Password too short'); return; }
users.push({ username, password });
res.status(201).send('User registered');
C. if (users.includes(username)) { res.status(201).send('User exists'); return; }
if (password.length < 8) { res.status(201).send('Password too short'); return; }
users.push({ username, password });
res.status(400).send('User registered');
D. if (users.includes(username)) { res.status(400).send('User exists'); }
if (password.length < 8) { res.status(400).send('Password too short'); }
users.push({ username, password });
res.status(201).send('User registered');
Solution
Step 1: Check for existing username
The code checks users.includes(username). If true, it responds with status 400 'User exists' and returns to prevent duplicates.
Step 2: Validate password length
The code checks password.length < 8. If true, responds with 400 'Password too short' and returns.
Step 3: Add user and send success response
After validations, users.push({ username, password }) and sends 201 'User registered'.
Final Answer:
if (users.includes(username)) { res.status(400).send('User exists'); return; }
if (password.length < 8) { res.status(400).send('Password too short'); return; }
users.push({ username, password });
res.status(201).send('User registered'); -> Option A
Quick Check:
Check user exists + password length, return on error [OK]
Hint: Return immediately after validation errors to stop flow [OK]