Consider an Express app that uses modular routing by splitting routes into separate files. What is the main effect on the app's behavior?
const express = require('express'); const app = express(); const userRoutes = require('./routes/users'); app.use('/users', userRoutes); app.listen(3000);
Think about how Express uses routers to handle specific paths.
Modular routing lets you split route handling into separate files, making the app easier to manage. The app uses the router module for requests starting with '/users'.
Which option correctly exports an Express router from a separate file?
const express = require('express');
const router = express.Router();
// Define routes here
// Export routerRemember the correct Node.js syntax for exporting modules.
In CommonJS (Node.js), you export a module using module.exports = .... Option D is correct.
Given this modular route file, why does the app not respond to '/products' requests?
const express = require('express'); const router = express.Router(); router.get('/products', (req, res) => { res.send('Product list'); }); module.exports = router;
Check how the router is mounted in the main app and how paths combine.
If the router is mounted without a path prefix, the route '/products' inside the router expects the full path '/products'. If the main app uses app.use(router);, requests to '/products' will work. But if the main app uses app.use('/products', router);, then inside the router the path should be '/' to respond to '/products'.
What is the main benefit of using modular routing in large Express applications?
Think about how splitting code into parts helps developers.
Modular routing mainly helps organize code, making it easier to read, maintain, and scale. It does not directly affect performance or security.
Given the main app mounts /api router, and inside that router another router handles /users, what URL path triggers the nested router's route?
/* main.js */ const express = require('express'); const app = express(); const apiRouter = require('./routes/api'); app.use('/api', apiRouter); app.listen(3000); /* routes/api.js */ const express = require('express'); const router = express.Router(); const usersRouter = require('./users'); router.use('/users', usersRouter); module.exports = router; /* routes/users.js */ const express = require('express'); const router = express.Router(); router.get('/', (req, res) => { res.send('Users list'); }); module.exports = router;
Consider how nested routers combine their paths.
The main app mounts /api, inside that the /users router is mounted, and inside that the route is /. So the full path is /api/users/, which matches option B.