Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of separating routes into different files in an Express app?
easy
A. It makes the app run faster.
B. It keeps the app organized and easier to maintain.
C. It automatically secures the routes.
D. It removes the need for middleware.

Solution

  1. Step 1: Understand code organization

    Separating routes into files helps keep code clean and manageable as the app grows.
  2. Step 2: Identify benefits

    This separation does not affect speed or security directly but improves maintainability.
  3. Final Answer:

    It keeps the app organized and easier to maintain. -> Option B
  4. Quick Check:

    Organized code = easier maintenance [OK]
Hint: Think about code clarity and maintenance benefits [OK]
Common Mistakes:
  • Confusing organization with performance improvements
  • Assuming route separation adds security automatically
  • Thinking middleware is not needed after separation
2. Which of the following is the correct way to create a router in a separate file named users.js?
easy
A. const router = express.Router(); module.exports = router;
B. const router = new express.Router(); export default router;
C. const router = express.Router; module.exports = router();
D. const router = express.Router(); export router;

Solution

  1. Step 1: Create router instance correctly

    Use express.Router() to create a new router instance.
  2. Step 2: Export the router properly

    Use module.exports = router; to export it for use in other files.
  3. Final Answer:

    const router = express.Router(); module.exports = router; -> Option A
  4. Quick Check:

    Correct router creation and export = const router = express.Router(); module.exports = router; [OK]
Hint: Remember to call Router() and use module.exports [OK]
Common Mistakes:
  • Forgetting parentheses after Router
  • Mixing CommonJS and ES module syntax
  • Exporting router incorrectly
3. Given the following code in users.js:
const express = require('express');
const router = express.Router();
router.get('/profile', (req, res) => {
  res.send('User Profile');
});
module.exports = router;
And in app.js:
const express = require('express');
const app = express();
const usersRouter = require('./users');
app.use('/users', usersRouter);
app.listen(3000);
What will be the response when a client requests /users/profile?
medium
A. User Profile
B. Cannot GET /users/profile
C. 404 Not Found error
D. Server crashes with error

Solution

  1. Step 1: Understand route prefixing

    The router is mounted at /users, so /profile inside router becomes /users/profile.
  2. Step 2: Check route handler response

    The route /profile sends 'User Profile' as response.
  3. Final Answer:

    User Profile -> Option A
  4. Quick Check:

    Route prefix + handler response = 'User Profile' [OK]
Hint: Remember mounted path prefixes router routes [OK]
Common Mistakes:
  • Forgetting the prefix from app.use
  • Expecting route at root instead of prefixed path
  • Assuming 404 without checking router setup
4. You have this code in products.js:
const express = require('express');
const router = express.Router();
router.get('/list', (req, res) => {
  res.send('Product List');
});
// Missing module.exports here
And in app.js:
const express = require('express');
const app = express();
const productsRouter = require('./products');
app.use('/products', productsRouter);
app.listen(3000);
What error will occur when starting the app or accessing /products/list?
medium
A. SyntaxError due to missing export
B. TypeError: Cannot read property 'use' of undefined
C. No error, but route won't respond
D. productsRouter is undefined, causing an error

Solution

  1. Step 1: Check module export in products.js

    Since module.exports is missing, require('./products') returns undefined.
  2. Step 2: Understand impact in app.js

    Using undefined as router in app.use causes an error when app tries to use it.
  3. Final Answer:

    productsRouter is undefined, causing an error -> Option D
  4. Quick Check:

    Missing export = undefined import error [OK]
Hint: Always export router to avoid undefined imports [OK]
Common Mistakes:
  • Assuming app runs without export
  • Confusing syntax error with runtime error
  • Not checking module.exports presence
5. You want to separate routes for /api/users and /api/products into two files: users.js and products.js. Each file exports a router with routes starting at /. How should you mount these routers in app.js to correctly handle requests to /api/users/profile and /api/products/list?
hard
A. app.use('/api', usersRouter); app.use('/api', productsRouter);
B. app.use('/users', usersRouter); app.use('/products', productsRouter);
C. app.use('/api/users', usersRouter); app.use('/api/products', productsRouter);
D. app.use('/', usersRouter); app.use('/', productsRouter);

Solution

  1. Step 1: Understand router base paths

    Each router handles routes starting at /, so mounting them at specific paths adds prefixes.
  2. Step 2: Mount routers with full API paths

    Mount usersRouter at /api/users and productsRouter at /api/products to match desired URLs.
  3. Final Answer:

    app.use('/api/users', usersRouter); app.use('/api/products', productsRouter); -> Option C
  4. Quick Check:

    Mount routers at full paths for correct URL handling [OK]
Hint: Mount routers at full URL prefixes to match routes [OK]
Common Mistakes:
  • Mounting routers at wrong or incomplete paths
  • Mounting both routers at same path causing conflicts
  • Assuming routers handle full paths internally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard