I have a models folder with an index.js file that looks like the following:
'use strict';
const {Dest1} = require('./destinations/dest1');
const {Dest2} = require('./destinations/dest2');
module.exports = {Dest1, Dest2};
I would like to dynamically load these objects based on a condition. I was thinking it might be interesting to have a middleware function that appends a value to the request that I could use to look up the correct object. I could just dynamically load the path, but I'm curious if this is possible.
Middleware:
function checkDestination(req,res,next){
if('destination1' in req.body){
req.path = 'Dest1'
}
next()
}
Router:
router.get('/', checkDestination, (req,res)=>{
//convert req.path to variable name here
const {Dest1} = require('./models')
})