0
0
Expressframework~20 mins

Mounting routers with app.use in Express - Practice Problems & Coding Challenges

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 a mounted router path?
Consider this Express app code:
const express = require('express');
const app = express();
const router = express.Router();

router.get('/hello', (req, res) => {
  res.send('Hello from router!');
});

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

app.listen(3000);

What will the server respond with when a client requests /api/hello?
Express
const express = require('express');
const app = express();
const router = express.Router();

router.get('/hello', (req, res) => {
  res.send('Hello from router!');
});

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

app.listen(3000);
AServer crashes with an error
BResponds with 404 Not Found
CResponds with 'Cannot GET /api/hello'
DResponds with 'Hello from router!'
Attempts:
2 left
💡 Hint
Think about how the router is mounted with the '/api' prefix and the route inside the router.
📝 Syntax
intermediate
1:30remaining
Which option correctly mounts a router at '/users'?
Given a router created with const router = express.Router();, which code correctly mounts it on the app at path '/users'?
Express
const express = require('express');
const app = express();
const router = express.Router();
Aapp.use('/users', router);
Bapp.get('/users', router);
Capp.router('/users', router);
Dapp.mount('/users', router);
Attempts:
2 left
💡 Hint
Remember the method used to add middleware or routers to an Express app.
🔧 Debug
advanced
2:00remaining
Why does this mounted router not respond as expected?
Look at this code:
const express = require('express');
const app = express();
const router = express.Router();

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

app.use(router);

app.listen(3000);

Why does a request to /profile return 404 Not Found?
Express
const express = require('express');
const app = express();
const router = express.Router();

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

app.use(router);

app.listen(3000);
ABecause the router is mounted without a path, so '/profile' is matched at root and should work
BBecause the router is mounted without a path, so '/profile' is not matched
CBecause the router's route should be '/router/profile' to work
DBecause app.use(router) requires a second argument for the path
Attempts:
2 left
💡 Hint
Think about what happens when you mount a router without specifying a path.
state_output
advanced
2:00remaining
What is the output when multiple routers are mounted with overlapping paths?
Given this code:
const express = require('express');
const app = express();
const router1 = express.Router();
const router2 = express.Router();

router1.get('/item', (req, res) => {
  res.send('From router1');
});

router2.get('/item', (req, res) => {
  res.send('From router2');
});

app.use('/api', router1);
app.use('/api', router2);

app.listen(3000);

What will the server respond with when a client requests /api/item?
Express
const express = require('express');
const app = express();
const router1 = express.Router();
const router2 = express.Router();

router1.get('/item', (req, res) => {
  res.send('From router1');
});

router2.get('/item', (req, res) => {
  res.send('From router2');
});

app.use('/api', router1);
app.use('/api', router2);

app.listen(3000);
AResponds with both 'From router1' and 'From router2'
BResponds with 'From router2'
CResponds with 'From router1'
DResponds with 404 Not Found
Attempts:
2 left
💡 Hint
Consider the order in which middleware and routers are added in Express.
🧠 Conceptual
expert
2:30remaining
What happens if you mount a router with a trailing slash in the path?
Consider this code:
const express = require('express');
const app = express();
const router = express.Router();

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

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

app.listen(3000);

What is the effect of the trailing slash in app.use('/api/', router) on route matching?
Express
const express = require('express');
const app = express();
const router = express.Router();

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

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

app.listen(3000);
AThe trailing slash causes a syntax error when mounting the router
BThe trailing slash is ignored; '/api/data' matches and responds with 'Data route'
CThe trailing slash causes only '/api//data' to match, so '/api/data' returns 404
DThe trailing slash causes the router to be mounted at root '/' instead of '/api'
Attempts:
2 left
💡 Hint
Think about how Express normalizes paths when mounting routers.