0
0
Expressframework~20 mins

Why modular routing matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you use modular routing in Express?

Consider an Express app that uses modular routing by splitting routes into separate files. What is the main effect on the app's behavior?

Express
const express = require('express');
const app = express();
const userRoutes = require('./routes/users');
app.use('/users', userRoutes);
app.listen(3000);
AAll routes must be defined in the main app file; modular routing has no effect.
BThe app will crash because routes cannot be split into separate files in Express.
CThe app organizes routes better and handles requests to '/users' using the separate router module.
DThe app ignores the '/users' prefix and treats all routes as if they were in the main file.
Attempts:
2 left
💡 Hint

Think about how Express uses routers to handle specific paths.

📝 Syntax
intermediate
1:30remaining
Identify the correct way to export a router in Express modular routing

Which option correctly exports an Express router from a separate file?

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

// Define routes here

// Export router
Amodule.export = router;
Bexport default router;
Cexports.router = router;
Dmodule.exports = router;
Attempts:
2 left
💡 Hint

Remember the correct Node.js syntax for exporting modules.

🔧 Debug
advanced
2:30remaining
Why does this modular route not respond as expected?

Given this modular route file, why does the app not respond to '/products' requests?

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

router.get('/products', (req, res) => {
  res.send('Product list');
});

module.exports = router;
AThe router should use <code>router.get('/', ...)</code> instead of <code>router.get('/products', ...)</code>.
BThe main app must use <code>app.use('/products', router);</code> instead of <code>app.use(router);</code> to prefix routes correctly.
CThe router file is missing <code>express()</code> initialization.
DThe route handler must call <code>next()</code> after <code>res.send()</code>.
Attempts:
2 left
💡 Hint

Check how the router is mounted in the main app and how paths combine.

🧠 Conceptual
advanced
2:00remaining
Why is modular routing important for large Express apps?

What is the main benefit of using modular routing in large Express applications?

AIt helps keep code organized and easier to maintain by separating route logic into files.
BIt improves app performance by loading routes faster.
CIt automatically secures routes without extra code.
DIt allows the app to run without a main server file.
Attempts:
2 left
💡 Hint

Think about how splitting code into parts helps developers.

state_output
expert
3:00remaining
What is the output when using nested modular routers in Express?

Given the main app mounts /api router, and inside that router another router handles /users, what URL path triggers the nested router's route?

Express
/* 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;
AA GET request to '/users' will respond with 'Users list'.
BA GET request to '/api/users' will respond with 'Users list'.
CA GET request to '/api' will respond with 'Users list'.
DA GET request to '/api/users/' will cause a 404 error.
Attempts:
2 left
💡 Hint

Consider how nested routers combine their paths.