0
0
Expressframework~20 mins

Why REST principles matter in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
REST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use statelessness in REST APIs?
In REST, statelessness means each request from client to server must contain all the information needed to understand and process the request. Why is this important?
AIt forces the server to remember client sessions, making the API faster.
BIt allows the server to handle each request independently, improving scalability and reliability.
CIt requires the client to store all server data, reducing server load.
DIt makes the API stateful, so the server can track user progress easily.
Attempts:
2 left
💡 Hint
Think about how servers handle many users at once without mixing their data.
component_behavior
intermediate
2:00remaining
What happens if REST APIs don't use proper HTTP methods?
Consider a REST API built with Express that uses POST for all actions, including data retrieval. What is a likely consequence?
AClients and intermediaries cannot cache responses properly, reducing performance.
BThe API will automatically become faster because POST is always faster than GET.
CThe API will reject all requests due to method mismatch errors.
DThe server will store session data for each POST request.
Attempts:
2 left
💡 Hint
Think about how browsers and proxies handle GET vs POST requests.
state_output
advanced
2:00remaining
What is the output of this Express route following REST principles?
Given this Express route, what JSON response will the client receive when requesting GET /users/42? ```js const express = require('express'); const app = express(); const users = { 42: { id: 42, name: 'Alice' } }; app.get('/users/:id', (req, res) => { const user = users[req.params.id]; if (!user) { return res.status(404).json({ error: 'User not found' }); } res.json(user); }); // Assume server is running and client requests GET /users/42 ```
Express
const express = require('express');
const app = express();
const users = { 42: { id: 42, name: 'Alice' } };

app.get('/users/:id', (req, res) => {
  const user = users[req.params.id];
  if (!user) {
    return res.status(404).json({ error: 'User not found' });
  }
  res.json(user);
});
A{"id":42,"name":"Alice"}
B{"error":"User not found"}
C404 Not Found with empty body
D{"id":"42","name":"Alice"}
Attempts:
2 left
💡 Hint
Look at how the user object is retrieved and returned as JSON.
📝 Syntax
advanced
2:00remaining
Which Express route correctly implements RESTful DELETE for a resource?
Choose the correct Express route syntax to delete a user by id following REST principles.
Aapp.put('/users/:id/delete', (req, res) => { /* delete logic */ });
Bapp.post('/users/delete/:id', (req, res) => { /* delete logic */ });
Capp.get('/users/:id/delete', (req, res) => { /* delete logic */ });
Dapp.delete('/users/:id', (req, res) => { /* delete logic */ });
Attempts:
2 left
💡 Hint
REST uses HTTP DELETE method to remove resources.
🔧 Debug
expert
3:00remaining
Why does this Express REST API fail to scale properly?
Consider this Express server code snippet: ```js const express = require('express'); const app = express(); let sessionData = {}; app.post('/login', (req, res) => { const userId = req.body.userId; sessionData[userId] = { loggedIn: true }; res.json({ message: 'Logged in' }); }); app.get('/profile', (req, res) => { const userId = req.query.userId; if (sessionData[userId]?.loggedIn) { res.json({ profile: 'User profile data' }); } else { res.status(401).json({ error: 'Not logged in' }); } }); app.listen(3000); ``` What is the main reason this design violates REST principles and causes scaling problems?
Express
const express = require('express');
const app = express();
let sessionData = {};

app.post('/login', (req, res) => {
  const userId = req.body.userId;
  sessionData[userId] = { loggedIn: true };
  res.json({ message: 'Logged in' });
});

app.get('/profile', (req, res) => {
  const userId = req.query.userId;
  if (sessionData[userId]?.loggedIn) {
    res.json({ profile: 'User profile data' });
  } else {
    res.status(401).json({ error: 'Not logged in' });
  }
});

app.listen(3000);
AThe server does not validate userId format, causing security issues.
BThe server uses POST for login instead of GET, which is not allowed in REST.
CThe server stores session state in memory, breaking statelessness and preventing horizontal scaling.
DThe server listens on port 3000, which is reserved and causes conflicts.
Attempts:
2 left
💡 Hint
Think about what statelessness means for REST and how storing data in memory affects multiple servers.