What is the difference between passing an arrow function and regular function with "middleware" name in here?

Tell us what’s happening:

Originally I wrote the middleware code in the following way:

app.use((res, req, next) => {
  console.log(`${req.method} ${req.path} - ::ffff:${req.ip}`);
  next();
});

However, I was getting ‘undefined’ for the passed values from the ‘req’, when I changed my code to this:

app.use(function middleware(req, res, next) {
  console.log(`${req.method} ${req.path} - ::ffff:${req.ip}`);
  next();
});

It all worked as it was supposed. What is the difference in here? Aren’t arrow functions and regular functions supposed to be handled in exactly same way in this case?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Implement a Root-Level Request Logger Middleware

Link to the challenge:

You switched the parameter order of req and res. The parameter order matters.

In the arrow function, you have (res, req, next) and access req in the body.

In the function declaration version you have (req, res, next) and access req in the body.

1 Like

ahhh I see it now, idk how I missed it, thanks! :slight_smile: