Consider this Express app code that uses namespaces (routers) to separate routes:
const express = require('express');
const app = express();
const apiRouter = express.Router();
apiRouter.get('/users', (req, res) => res.send('API Users'));
const adminRouter = express.Router();
adminRouter.get('/users', (req, res) => res.send('Admin Users'));
app.use('/api', apiRouter);
app.use('/admin', adminRouter);
app.listen(3000);What will be the response when a client requests GET /admin/users?
const express = require('express'); const app = express(); const apiRouter = express.Router(); apiRouter.get('/users', (req, res) => res.send('API Users')); const adminRouter = express.Router(); adminRouter.get('/users', (req, res) => res.send('Admin Users')); app.use('/api', apiRouter); app.use('/admin', adminRouter); app.listen(3000);
Think about how the app.use prefixes the routes in each router.
The adminRouter is mounted at /admin. So the route /users inside adminRouter becomes /admin/users. When the client requests /admin/users, the handler in adminRouter responds with 'Admin Users'.
Which code snippet correctly creates a router namespace for /blog in Express?
Remember how to create a router instance in Express.
The correct way to create a router is by calling express.Router(). Option D does this correctly. Option D calls express() which creates an app, not a router. Option D misses parentheses, so it references the function but does not call it. Option D tries to use new which is not how Router is instantiated.
Given this Express code:
const express = require('express');
const app = express();
const userRouter = express.Router();
userRouter.get('/profile', (req, res) => res.send('User Profile'));
app.use('/user', userRouter);
app.listen(3000);When requesting GET /profile, the server responds with 404. Why?
const express = require('express'); const app = express(); const userRouter = express.Router(); userRouter.get('/profile', (req, res) => res.send('User Profile')); app.use('/user', userRouter); app.listen(3000);
Check the full path the route is mounted on.
The router is mounted at '/user', so all routes inside it are prefixed with '/user'. The route '/profile' inside the router becomes '/user/profile' in the app. Requesting '/profile' does not match any route, causing 404.
Consider this Express app:
const express = require('express');
const app = express();
const apiRouter = express.Router();
const v1Router = express.Router();
v1Router.use((req, res, next) => {
req.version = 'v1';
next();
});
v1Router.get('/data', (req, res) => {
res.send(`API version: ${req.version}`);
});
apiRouter.use('/v1', v1Router);
app.use('/api', apiRouter);
app.listen(3000);What will be the response body when a client requests GET /api/v1/data?
const express = require('express'); const app = express(); const apiRouter = express.Router(); const v1Router = express.Router(); v1Router.use((req, res, next) => { req.version = 'v1'; next(); }); v1Router.get('/data', (req, res) => { res.send(`API version: ${req.version}`); }); apiRouter.use('/v1', v1Router); app.use('/api', apiRouter); app.listen(3000);
Look at how middleware sets req.version before the route handler.
The middleware on v1Router sets req.version = 'v1' before the route handler runs. So when the client requests /api/v1/data, the response is 'API version: v1'.
Choose the correct statement about using namespaces (routers) in Express:
Think about how routers help organize routes and middleware.
Express routers can be nested and each router can have its own middleware and routes. This allows developers to separate routes into modules or namespaces. Middleware applies normally within routers. Options A, C, and D are incorrect because routers do support middleware, routes keep access to request properties, and query parameters are passed normally but not 'automatically merged' in a special way.