Consider this Express route setup for a resource /books. What will be the response when a GET request is made to /books/42?
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
Look at how req.params works in Express routes with parameters.
The route /books/:id captures the part after /books/ as req.params.id. So when you visit /books/42, req.params.id is 42. The handler sends back Book ID is 42.
You want to add a POST route to /users to create a new user. Which code snippet correctly sets this up in Express?
Think about which HTTP method is used to create resources and the typical URL pattern.
POST to /users is the standard way to create a new user resource. GET is for reading, PUT is for updating, and POST with an ID in the URL is unusual for creation.
Given this Express route, a GET request to /products/123 hangs and never sends a response. What is the cause?
app.get('/products/:id', (req, res) => { const productId = req.params.id; // Missing res.send or res.end });
Check if the handler sends a response back to the client.
The route handler reads the parameter but never calls res.send() or similar. Without sending a response, the client waits indefinitely.
Why do developers prefer grouping routes by resource (like /users, /orders) in Express applications?
Think about how organizing code affects readability and maintenance.
Grouping routes by resource helps developers find and update related routes easily. It does not affect performance, view generation, or security directly.
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?
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;
Count how many routes are defined and increment routesCount.
There are exactly 5 routes defined: GET /, POST /, GET /:id, PUT /:id, DELETE /:id. Each increments routesCount by 1, so final value is 5.