0
0
Expressframework~20 mins

next() function and flow control in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when next() is called in Express middleware?
Consider this Express middleware function:
app.use((req, res, next) => {
console.log('Middleware 1');
next();
console.log('After next');
});

What will be printed to the console when a request is made?
Express
app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
  console.log('After next');
});
ANo output
BMiddleware 1\nAfter next
CAfter next\nMiddleware 1
DMiddleware 1
Attempts:
2 left
💡 Hint
Think about what next() does and when the code after it runs.
lifecycle
intermediate
2:00remaining
How does next() affect error handling middleware?
Given these middleware functions:
app.use((req, res, next) => {
next('error');
});

app.use((err, req, res, next) => {
res.send('Error handled');
});

What will the client receive when a request is made?
Express
app.use((req, res, next) => {
  next('error');
});

app.use((err, req, res, next) => {
  res.send('Error handled');
});
AMiddleware skipped, next middleware runs
BNo response, request hangs
CError handled
DSyntax error
Attempts:
2 left
💡 Hint
What happens when next() is called with a string argument?
📝 Syntax
advanced
2:00remaining
Which middleware setup causes a runtime error?
Identify which option will cause an error when a request is made:
Aapp.use((req, res, next) => { throw new Error('Fail'); });
Bapp.use((req, res, next) => { next(); });
Capp.use((err, req, res, next) => { res.send('Error'); });
Dapp.use((req, res) => { res.send('Done'); next(); });
Attempts:
2 left
💡 Hint
Check if calling next() after sending a response causes issues.
🔧 Debug
advanced
2:00remaining
Why does this middleware never call next() or send a response?
Look at this middleware:
app.use((req, res, next) => {
if (req.query.pass) {
next();
}
});

What happens if req.query.pass is false or missing?
Express
app.use((req, res, next) => {
  if (req.query.pass) {
    next();
  }
});
ARequest hangs because neither next() nor res.send() is called
BRequest proceeds normally
CError thrown due to missing next()
DResponse sent automatically
Attempts:
2 left
💡 Hint
What happens if middleware neither calls next() nor sends a response?
🧠 Conceptual
expert
3:00remaining
What is the effect of calling next('route') in Express middleware?
Consider this route setup:
app.get('/test', (req, res, next) => {
next('route');
}, (req, res) => {
res.send('First handler');
});

app.get('/test', (req, res) => {
res.send('Second handler');
});

What response will the client receive when requesting '/test'?
Express
app.get('/test', (req, res, next) => {
  next('route');
}, (req, res) => {
  res.send('First handler');
});

app.get('/test', (req, res) => {
  res.send('Second handler');
});
ASecond handler
BFirst handler
CError: route not found
DRequest hangs
Attempts:
2 left
💡 Hint
What does next('route') do inside route middleware?