Home > OS >  NodeJs callback and parameters
NodeJs callback and parameters

Time:06-13

I came across this piece of code which needs some clarification. http://expressjs.com/en/guide/writing-middleware.html#:~:text=The next () function is not a part,“next”. To avoid confusion, always use this convention. Refer section : "Configurable middleware"

How is it possible that "req", "res" and "next" can be referred inside the exported function directly ? I mean neither the caller "app.use()" nor the CB export definition "module.exports" = function(options){ }" are having any references to them.

----- block using an exported middleware ----

const mw = require('./my-middleware.js');    
app.use(mw({ option1: '1', option2: '2' }))

}

------ Exported Function --------

module.exports = function (options) {

  // Please explain the below access/visibility of req, res and next parameters.
  // I do not see them passed/referred/handed-down from app.use() level.
  return function (req, res, next) {
    // Implement the middleware function based on the options object
    next()
  }

CodePudding user response:

Every express middleware function has access to request, response, and a callback function, conventionally named as next.

How does a middleware have access to these? When you register a function as a middleware, express passes that middleware function the above mentioned parameters as arguments.

So, when you create a function with the intention of it being used as a middleware, you already know that it will receive three arguments from express. As a result, you create a function with the 3 parameters mentioned above:

function myMiddleware(req, res, next) {
  ...
}

Sometimes, we want the middleware function to be configurable; so we create a function that takes some configuration options and then return a function that will be used as a middleware function.

And as we know that each middleware function will be passed 3 arguments by express, we define the inner function to take 3 parameters:

function returnConfigurableMiddleware(options) {
   ...
   return (req, res, next) => {
      ...
   }
}

In this case, to register the middleware, we have to call the function that returns the function to be used as a middleware.

app.use(returnConfigurableMiddleware(...))

CodePudding user response:

Try to think this way: The Exported Function defines a function which accepts (req, res, next) as parameter.

Normally When you define a function there are no restrictions on how you define the parameters.

But, in this case, the parameters has to be (req, res, next) because you are defining a express middleware

CodePudding user response:

neither the caller [...] nor the CB export definition [...] are having any references to them

That's because nothing in the code shown is ever calling that function. You're exporting a function that returns a function. The outer function is being called here:

mw({ option1: '1', option2: '2' })

That returns a function, but doesn't call that function. It just passes it as the only parameter to app.use().

Based on the usage, presumably app is an Express object, and it's .use() function is how you build middleware for the Express web server.

So, internal to the workings of Express, as part of the HTTP request/response pipeline, it will call your function (req, res, next) at some later time. And when it does, it will pass (at least) three arguments to that function.

  • Related