I have 4 controllers for 4 different situations. I am trying to call the 4 controllers from the same endpoint depending on the different request queries. I have implemented the logic as below:
const express = require('express');
const quesController = require('../controllers/quesController');
const router = express.Router();
router.route('/').get((req, res) => {
const topic = req.query.topic.toLowerCase();
const difficulty = req.query.difficulty.toLowerCase();
if (topic === 'random' && difficulty === 'random') {
quesController.getQuestionRandom(req, res);
} else if (topic === 'random') {
quesController.getQuestionByDiff(req, res);
} else if (difficulty === 'random') {
quesController.getQuestionByTopic(req, res);
} else {
quesController.getSpecificQuestion(req, res);
}
});
module.exports = router;
Is there any other way to do this type of conditional routing?