I have an express application that i'm trying to put all my middleware in it's own file. Some of the middleware functions need the db object and some don't.
It's pretty straight forward for the functions that don't need the db object, but given my code structure below, how can I reference the db object in doesNotNeedDbParam since it already has params req, res, and next?
somefile.js
from Requiring file with some functions that need a param
It's pretty straight forward for the functions that don't need the db object, but given my code structure below, how can I reference the db object in doesNotNeedDbParam since it already has params req, res, and next?
somefile.js
const router = express.Router()
const doesNotNeedDbParam = require('./middleware')().doesNotNeedDbParam
function foo () {
// Currently I have to call require and pass in the db object here b/c it's
not set when requiring the function doesNotNeedDbPara
router.use(require('./middleware')(db).needsDbParam // <-- Is there a better
way to do this so that I can require the file above and pass the db object in when it's set?
}
// Setup db object here
foo()
middleware.jsfunction doesNotNeedDbParam (req, res, next) {
...
}
function needsDbParam (req, res, next) {
// Where do i reference the db variable?
}
module.exports = (db) => {
return {
doesNotNeedDbParam: doesNotNeedDbParam,
needsDbParam: needsDbParam
}
}
from Requiring file with some functions that need a param
No comments:
Post a Comment