0
0
Expressframework~20 mins

app.all and app.use for catch-all in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Catch-All Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using app.all for a catch-all route?

Consider this Express code snippet:

const express = require('express');
const app = express();

app.all('*', (req, res) => {
  res.send('Catch-all with app.all');
});

app.listen(3000);

What will a GET request to /anything respond with?

Express
const express = require('express');
const app = express();

app.all('*', (req, res) => {
  res.send('Catch-all with app.all');
});

app.listen(3000);
AThe server throws an error because app.all cannot use '*' as a path.
BThe server responds with 404 Not Found because no GET route is defined.
CThe server responds with 'Catch-all with app.all' only for POST requests.
DThe server responds with 'Catch-all with app.all' for any HTTP method and path.
Attempts:
2 left
💡 Hint

Think about what app.all does with the path '*'.

component_behavior
intermediate
2:00remaining
How does app.use behave as a catch-all middleware?

Given this Express setup:

const express = require('express');
const app = express();

app.use((req, res) => {
  res.send('Catch-all with app.use');
});

app.listen(3000);

What happens when a GET request is made to /random?

Express
const express = require('express');
const app = express();

app.use((req, res) => {
  res.send('Catch-all with app.use');
});

app.listen(3000);
AThe server responds only if the request method is POST.
BThe server responds with 404 Not Found because no route matches.
CThe server responds with 'Catch-all with app.use' for any path and method.
DThe server throws an error because app.use requires a path argument.
Attempts:
2 left
💡 Hint

Remember that app.use without a path matches all requests.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error when trying to create a catch-all route?

Which of these Express route definitions will cause a syntax error?

Aapp.all('*', (req, res) => res.send('All methods catch-all'));
Bapp.all((req, res) => res.send('Missing path argument'));
Capp.use((req, res) => res.send('Use without path catch-all'));
Dapp.use('*', (req, res) => res.send('Use with path catch-all'));
Attempts:
2 left
💡 Hint

Check if all required arguments are present for app.all.

state_output
advanced
2:00remaining
What is the response order when both app.use and app.all catch-all handlers exist?

Consider this Express code:

const express = require('express');
const app = express();

app.use((req, res, next) => {
  res.write('Use start - ');
  next();
});

app.all('*', (req, res) => {
  res.end('All end');
});

app.listen(3000);

What will a GET request to /test respond with?

Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  res.write('Use start - ');
  next();
});

app.all('*', (req, res) => {
  res.end('All end');
});

app.listen(3000);
AThe response is 'Use start - All end'.
BThe response is 'All end' only.
CThe response is 'Use start - ' only, and the request hangs.
DThe server throws an error because res.write and res.end cannot be mixed.
Attempts:
2 left
💡 Hint

Think about how middleware passes control with next().

🔧 Debug
expert
3:00remaining
Why does this catch-all middleware never respond?

Look at this Express code:

const express = require('express');
const app = express();

app.use((req, res, next) => {
  next();
  res.send('Catch-all response');
});

app.listen(3000);

Why does a request to any path never get the 'Catch-all response'?

Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  next();
  res.send('Catch-all response');
});

app.listen(3000);
ABecause calling next() passes control and the response is sent after, which is ignored.
BBecause res.send must be called before next(), otherwise it causes a syntax error.
CBecause app.use requires a path argument to respond.
DBecause res.send cannot be used inside middleware.
Attempts:
2 left
💡 Hint

Think about the order of next() and res.send() calls.