developerasun / myCodeBox-web

Open source code box for web developers.
Apache License 2.0
5 stars 0 forks source link

[RESEARCH] Express/middleware : custom parameter #222

Open developerasun opened 2 years ago

developerasun commented 2 years ago

topic : understanding how to add custom parameters in Express middleware

read this

what if you need other input parameters for you business logic deviating from (req, res, next)? For example, if you want to hand over some configuration values to feed your logic? To achieve this, you can use a simple but efficient pattern: wrap your actual middleware function with a second one that receives the desired parameters, like so.

see codes

const paramMiddleware = (myParam) => {
  return (req, res, next) => {
    // implement your business logic using 'myParam'
    // ...
    next();
  }
}

reference