Bird
Raised Fist0
Expressframework~20 mins

Creating an Express Router - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express Router 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 router code below, what response will the server send when a client requests the '/users' path?
Express
const express = require('express');
const router = express.Router();

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

module.exports = router;
AThe server responds with 'User list'.
BThe server responds with a 404 Not Found error.
CThe server responds with 'users'.
DThe server crashes with a runtime error.
Attempts:
2 left
💡 Hint
Check the route path and the response sent inside the router.get handler.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates and exports an Express router?
Select the code snippet that correctly creates an Express router and exports it for use in other files.
A
const express = require('express');
const router = new express.Router();
module.exports = router;
B
const express = require('express');
const router = express.Router();
module.exports = router;
C
const express = require('express');
const router = express.Router;
module.exports = router;
D
const router = express();
module.exports = router;
Attempts:
2 left
💡 Hint
Remember to call express.Router() as a function to create the router instance.
🔧 Debug
advanced
2:00remaining
Why does this Express router code cause a runtime error?
Examine the code below. Why will this cause an error when the server runs?
Express
const express = require('express');
const router = express.Router();

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

module.exports = router;
AThe variable 'items' is not defined, causing a ReferenceError.
BThe route path '/items' is invalid and causes a syntax error.
CThe router is not exported correctly, causing a module error.
DThe res.send method is used incorrectly and causes a TypeError.
Attempts:
2 left
💡 Hint
Check if all variables used inside the route handler are declared.
❓ state_output
advanced
2:00remaining
What is the response after adding middleware to the router?
Consider this router code with middleware. What will the client receive when requesting '/data'?
Express
const express = require('express');
const router = express.Router();

router.use((req, res, next) => {
  req.customData = 'Hello';
  next();
});

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

module.exports = router;
AThe server throws a TypeError.
BThe client receives undefined.
CThe client receives 'Hello'.
DThe client receives an empty response.
Attempts:
2 left
💡 Hint
Middleware adds a property to the request object before the route handler runs.
🧠 Conceptual
expert
3:00remaining
What happens if you mount the same router twice with different base paths?
If you have a router with a route '/info' and you mount it on an Express app at '/a' and '/b', what URLs will respond with the router's '/info' handler?
Express
const express = require('express');
const app = express();
const router = express.Router();

router.get('/info', (req, res) => {
  res.send('Info page');
});

app.use('/a', router);
app.use('/b', router);
ANeither will respond; mounting twice causes a conflict error.
BOnly '/a/info' will respond; '/b/info' will 404.
COnly '/b/info' will respond; '/a/info' will 404.
DBoth '/a/info' and '/b/info' will respond with 'Info page'.
Attempts:
2 left
💡 Hint
Think about how Express mounts routers at different base paths.

Practice

(1/5)
1. What is the main purpose of using express.Router() in an Express app?
easy
A. To handle database connections
B. To start the Express server
C. To create a modular set of routes that can be mounted separately
D. To serve static files

Solution

  1. Step 1: Understand what express.Router() does

    This function creates a new router object that can hold routes separately from the main app.
  2. Step 2: Identify the purpose of modular routes

    Modular routes help organize code by grouping related routes, making the app easier to maintain.
  3. Final Answer:

    To create a modular set of routes that can be mounted separately -> Option C
  4. Quick Check:

    Router = modular routes [OK]
Hint: Router organizes routes separately for cleaner code [OK]
Common Mistakes:
  • Thinking router starts the server
  • Confusing router with database handling
  • Assuming router serves static files
2. Which of the following is the correct way to create a new Express router?
easy
A. const router = express.Router();
B. const router = new express.Router();
C. const router = express.createRouter();
D. const router = Router();

Solution

  1. Step 1: Recall the Express Router creation syntax

    The correct method is calling Router() as a function on the express object.
  2. Step 2: Check each option for syntax correctness

    const router = express.Router(); uses express.Router() correctly. const router = new express.Router(); wrongly uses new. const router = express.createRouter(); uses a non-existent method. const router = Router(); calls Router() without express..
  3. Final Answer:

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

    Correct syntax = express.Router() [OK]
Hint: Use express.Router() without new keyword [OK]
Common Mistakes:
  • Using new keyword with Router
  • Calling a non-existent createRouter method
  • Calling Router without express prefix
3. Given this code snippet, what will be the output when a GET request is made to /api/users?
const express = require('express');
const app = express();
const router = express.Router();

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

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

app.listen(3000);
medium
A. Cannot GET /api/users
B. Error: router not defined
C. 404 Not Found
D. User list

Solution

  1. Step 1: Understand route mounting with app.use('/api', router)

    This mounts the router under the path /api, so routes inside router are prefixed with /api.
  2. Step 2: Check the router route /users

    The router handles GET requests to /users, so combined with the mount path, the full path is /api/users.
  3. Final Answer:

    User list -> Option D
  4. Quick Check:

    Mounted path + router path = /api/users [OK]
Hint: Router path combines with app.use prefix [OK]
Common Mistakes:
  • Forgetting the mount path prefix
  • Expecting route at root instead of /api
  • Assuming router not mounted
4. Identify the error in this Express router code:
const express = require('express');
const router = express.Router;

router.get('/info', (req, res) => {
  res.send('Info page');
});

module.exports = router;
medium
A. Missing parentheses when creating router instance
B. Route path should be '/info/' with trailing slash
C. res.send should be res.json
D. module.exports should export an object, not router

Solution

  1. Step 1: Check router creation syntax

    The code uses express.Router without parentheses, so it assigns the function itself, not an instance.
  2. Step 2: Confirm correct router instantiation

    It should be express.Router() to create a router instance that supports route methods.
  3. Final Answer:

    Missing parentheses when creating router instance -> Option A
  4. Quick Check:

    Router needs () to create instance [OK]
Hint: Router must be called as a function with () [OK]
Common Mistakes:
  • Forgetting () after express.Router
  • Thinking trailing slash is required
  • Confusing res.send with res.json
5. You want to create two routers: one for /users and one for /products. How do you correctly mount them in your Express app so that requests to /users/list and /products/list work as expected?
hard
A. app.use(usersRouter); app.use(productsRouter);
B. app.use('/users', usersRouter); app.use('/products', productsRouter);
C. app.use('/list', usersRouter); app.use('/list', productsRouter);
D. app.get('/users', usersRouter); app.get('/products', productsRouter);

Solution

  1. Step 1: Understand router mounting with path prefixes

    To handle routes under /users and /products, mount each router with those paths using app.use().
  2. Step 2: Check each option for correct usage

    app.use('/users', usersRouter); app.use('/products', productsRouter); mounts routers correctly with prefixes. app.use(usersRouter); app.use(productsRouter); mounts without prefixes, so routes won't be under desired paths. app.use('/list', usersRouter); app.use('/list', productsRouter); mounts both under /list, causing conflicts. app.get('/users', usersRouter); app.get('/products', productsRouter); uses app.get incorrectly with routers.
  3. Final Answer:

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

    Mount routers with correct path prefixes [OK]
Hint: Use app.use with path prefix for each router [OK]
Common Mistakes:
  • Mounting routers without path prefix
  • Using app.get instead of app.use for routers
  • Mounting multiple routers on same path
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