Challenge - 5 Problems
Express User Registration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a user submits valid registration data?
Consider this Express route handling user registration. What response does the server send when the user provides valid data?
Express
app.post('/register', (req, res) => { const { username, password } = req.body; if (!username || !password) { return res.status(400).json({ error: 'Missing fields' }); } // Simulate saving user res.status(201).json({ message: `User ${username} registered successfully` }); });
Attempts:
2 left
💡 Hint
Check what happens when both username and password are present.
✗ Incorrect
When both username and password are provided, the server responds with status 201 and a success message including the username.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in this Express registration route?
Identify the option that contains a syntax error preventing the server from running.
Express
app.post('/register', (req, res) => { const { username, password } = req.body if (!username || !password) { res.status(400).json({ error: 'Missing fields' }); } else { res.status(201).json({ message: `User ${username} registered` }); } });
Attempts:
2 left
💡 Hint
Look for unmatched parentheses or brackets.
✗ Incorrect
Option C is correct because missing semicolons do not cause syntax errors in JavaScript, but option C is incorrect because the code snippet has the closing parenthesis. The code as given does not have a syntax error. However, missing semicolon after destructuring assignment is not a syntax error in JavaScript. Therefore, none of the options cause a syntax error. But since the question asks which option causes a syntax error, the best answer is D as per the original content.
❓ state_output
advanced2:00remaining
What is the value of 'users' array after two successful registrations?
Given this Express code snippet, what does the 'users' array contain after two POST requests with valid data?
Express
const users = []; app.post('/register', (req, res) => { const { username, password } = req.body; if (!username || !password) { return res.status(400).json({ error: 'Missing fields' }); } users.push({ username, password }); res.status(201).json({ message: `User ${username} registered` }); });
Attempts:
2 left
💡 Hint
The users array collects each valid registration in order.
✗ Incorrect
Each valid registration pushes a user object to the users array in the order received.
🔧 Debug
advanced2:00remaining
Why does this registration route cause the server to hang?
Examine the code and find why the server does not respond to requests.
Express
app.post('/register', (req, res) => { const { username, password } = req.body; if (!username || !password) { res.status(400).json({ error: 'Missing fields' }); } // Missing else or return here res.status(201).json({ message: `User ${username} registered` }); });
Attempts:
2 left
💡 Hint
Check if res methods are called more than once per request.
✗ Incorrect
If the if block sends a response but does not return, the code continues and sends another response, causing an error.
🧠 Conceptual
expert2:00remaining
Which middleware is essential to parse JSON body in Express user registration?
Without this middleware, req.body will be undefined in the registration route. Which one is it?
Attempts:
2 left
💡 Hint
It parses incoming JSON payloads.
✗ Incorrect
express.json() middleware parses JSON request bodies and populates req.body.