0
0
Expressframework~20 mins

Resource-based route organization in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Resource Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express route handler?

Consider this Express route setup for a resource /books. What will be the response when a GET request is made to /books/42?

Express
const express = require('express');
const app = express();

app.get('/books/:id', (req, res) => {
  res.send(`Book ID is ${req.params.id}`);
});

// Assume app.listen is called elsewhere
A404 Not Found
BBook ID is 42
CError: Cannot read property 'id' of undefined
DBook ID is :id
Attempts:
2 left
💡 Hint

Look at how req.params works in Express routes with parameters.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a resource route for creating a new user?

You want to add a POST route to /users to create a new user. Which code snippet correctly sets this up in Express?

Aapp.post('/users', (req, res) => { res.send('User created'); });
Bapp.get('/users/create', (req, res) => { res.send('User created'); });
Capp.put('/users', (req, res) => { res.send('User created'); });
Dapp.post('/users/:id', (req, res) => { res.send('User created'); });
Attempts:
2 left
💡 Hint

Think about which HTTP method is used to create resources and the typical URL pattern.

🔧 Debug
advanced
2:00remaining
Why does this resource route handler not respond as expected?

Given this Express route, a GET request to /products/123 hangs and never sends a response. What is the cause?

Express
app.get('/products/:id', (req, res) => {
  const productId = req.params.id;
  // Missing res.send or res.end
});
AThe handler does not send a response, so the request hangs.
BThe route path is incorrect; it should be '/product/:id'.
Creq.params.id is undefined causing an error.
DExpress requires next() to be called to complete the request.
Attempts:
2 left
💡 Hint

Check if the handler sends a response back to the client.

🧠 Conceptual
advanced
2:00remaining
What is the main benefit of organizing routes by resource in Express?

Why do developers prefer grouping routes by resource (like /users, /orders) in Express applications?

AIt prevents unauthorized access by restricting routes to resource folders.
BIt improves server performance by caching resource routes automatically.
CIt allows Express to automatically generate HTML views for each resource.
DIt makes the code easier to maintain and understand by keeping related routes together.
Attempts:
2 left
💡 Hint

Think about how organizing code affects readability and maintenance.

state_output
expert
3:00remaining
What is the final value of the variable routesCount after this Express router setup?

Given this code that organizes routes for a resource, what is the value of routesCount after all routes are added?

Express
const express = require('express');
const router = express.Router();

let routesCount = 0;

router.get('/', (req, res) => res.send('List'));
routesCount++;

router.post('/', (req, res) => res.send('Create'));
routesCount++;

router.get('/:id', (req, res) => res.send('Read'));
routesCount++;

router.put('/:id', (req, res) => res.send('Update'));
routesCount++;

router.delete('/:id', (req, res) => res.send('Delete'));
routesCount++;

module.exports = router;
A6
B4
C5
D0
Attempts:
2 left
💡 Hint

Count how many routes are defined and increment routesCount.