0

I see so often people use regular functions for their middleware functions in the Express servers. Is there a reason for no arrow functions?

Is it because of hoisting reasons, because I've seen other posts on this but they don't break the middleware out to its own function

Tired both of them, and saw no issue really

  • 1
    The differences between arrow functions and regular functions are largely irrelevant for most middleware implementations. The main difference has to do with accessing the lexical scope value of `this` which is usually meaningless in middleware as `this` is generally not used in Express middleware since the handlers are typically stand-alone functions, not part of some object where you need access to instance data. The object data comes from `req` and `res` which are function parameters. – jfriend00 Feb 02 '23 at 22:59
  • It's not a perfect dupe, but just to reiterate what jfriend00 said, you will rarely run into middleware or handlers that make use of `this` in the express context. – Matt Feb 02 '23 at 23:44

0 Answers0