0
0
Expressframework~20 mins

Creating an Express Router - Practice Exercises

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