FAQ: Middleware - Control Flow With next()

This community-built FAQ covers the “Control Flow With next()” exercise from the lesson “Middleware”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Express

FAQs on the exercise Control Flow With next()

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

what do you mean when adding a parameter for next() function
next(‘Bean with that name does not exist’);

3 Likes

I wonder the same…

The explanation in this exercise is not well-written. We haven’t seen an argument passed to a next() function yet, but it’s not explained. Here’s what the Express documentation says:

If you pass anything to the next() function (except the string 'route' ), Express regards the current request as being an error and will skip any remaining non-error handling routing and middleware functions.

Fair enough, but I’m not sure how that applies to this first piece of middleware in the exercise, which is invoked by every request, regardless of route. It also seems redundant, given the response to ‘no beans found’ in the if statement. What am I missing?

3 Likes

I have the same questions.

Does any one form codecademy ever look at these questions?

Disappointing.

Mark

1 Like
pp.use('/beans/:beanName', (req, res, next) => {
  const beanName = req.params.beanName;
  if (!jellybeanBag[beanName]) {
    res.status(404).send('Bean with that name does not exist');
    return console.log('Response Sent');
  }
  req.bean = jellybeanBag[beanName];
  req.beanName = beanName;
  next();
});

if we don’t add next() in the last line. What will happen?
Are we adding it so that then only it would check for other route handlers with the same request?

I removed the argument in next() in the first app.use

app.use((req, res, next) => {
  console.log(`${req.method} Request Received`);
//  next('Bag with that name does not exist');
  next();
});

As newsnerd3000 writes then next() is handing over executing to the next matching route.

If you enter “/beans” in PATH :

  1. app.use((req, res, next) is executed
    Console : GET Request Received

  2. next() handover to the next matching route in app.get(‘/beans/’, (req, res, next)
    Console : Response Sent
    Web browser (BODY):
    {
    “mystery”: {
    “number”: 4
    },
    “lemon”: {
    “number”: 5
    },
    “rootBeer”: {
    “number”: 25
    },
    “cherry”: {
    “number”: 3
    },
    “licorice”: {
    “number”: 1
    }
    }

  3. There are no next() in app.get(‘/beans/’, (req, res, next) and even with a next() there no more matching routes so execution stops.