0
0
Expressframework~20 mins

Separating routes into files in Express - Practice Problems & Coding Challenges

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 is the output when accessing the /users route?

Given the Express app setup below with routes separated into files, what will the server respond when a client requests /users?

Express
const express = require('express');
const app = express();
const usersRouter = require('./routes/users');

app.use('/users', usersRouter);

// routes/users.js
const express = require('express');
const router = express.Router();

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

module.exports = router;
AThe server throws a runtime error because of missing middleware.
BThe server responds with 'Cannot GET /users'.
CThe server responds with 'Welcome to users'.
DThe server responds with 'User list'.
Attempts:
2 left
💡 Hint

Check how the router handles the / path inside the /users prefix.

📝 Syntax
intermediate
1:30remaining
Which option correctly exports an Express router from a separate file?

Choose the correct way to export an Express router from a file named products.js.

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

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

// What is the correct export statement here?
Amodule.exports = router;
Bmodule.export = router;
Cexport default router;
Dexports = router;
Attempts:
2 left
💡 Hint

Remember how CommonJS modules export values.

🔧 Debug
advanced
2:00remaining
Why does the route handler not respond when using separated routes?

Consider this Express app setup:

const express = require('express');
const app = express();
const apiRouter = require('./routes/api');

app.use('/api', apiRouter);

// routes/api.js
const express = require('express');
const router = express.Router();

router.get('/data', (req, res) => {
  // missing res.send or res.end
});

module.exports = router;

What happens when a client requests /api/data?

AThe request hangs and eventually times out because no response is sent.
BThe server throws a syntax error on startup.
CThe server responds with an empty 200 OK response.
DThe server responds with a 404 Not Found error.
Attempts:
2 left
💡 Hint

Check if the route handler sends a response.

🧠 Conceptual
advanced
1:30remaining
What is the benefit of separating routes into files in Express?

Why do developers separate routes into different files when building Express applications?

ATo avoid using middleware functions in the main app file.
BTo improve server performance by loading routes faster.
CTo organize code better and keep route logic modular and maintainable.
DTo prevent Express from handling requests synchronously.
Attempts:
2 left
💡 Hint

Think about code organization and teamwork.

state_output
expert
2:30remaining
What is the output after mounting nested routers with middleware?

Given the following Express app and route files, what will be the output when a client requests /api/users/profile?

Express
const express = require('express');
const app = express();
const apiRouter = require('./routes/api');

app.use('/api', apiRouter);

// routes/api.js
const express = require('express');
const router = express.Router();
const usersRouter = require('./users');

router.use('/users', (req, res, next) => {
  req.customData = 'middleware data';
  next();
});
router.use('/users', usersRouter);

module.exports = router;

// routes/users.js
const express = require('express');
const router = express.Router();

router.get('/profile', (req, res) => {
  res.send(req.customData);
});

module.exports = router;
AThe server responds with 404 Not Found because /profile route is not found.
B"middleware data" is sent as the response.
C"undefined" is sent as the response because req.customData is missing.
DThe server throws an error because middleware is not properly chained.
Attempts:
2 left
💡 Hint

Consider how middleware modifies req and how nested routers handle requests.