I am creating an app for production use and would like to combine several routes using one switch statement. I currently use it in development and it works perfectly, however I have not seen this approach used before, and would like to know if there is a reason. Is there any issue using this approach? If so, really looking for the why in the answer.
This is what I'd like to do instead of creating multiple routes.
router.post('/save', auth, async (req, res)=>{
switch(req.body.action) {
case 'user':
result = await asyncSaveUser(req.body.data);
break;
case 'order':
result = await asyncSaveOrder(req.body.data);
break;
default:
result = {success:false, data: 'not valid action'};
break;
}
return res.status(200).json(result);
})
In the API I would create an action.
url: {baseUrl}+'/save'
body: {
"action":"user",
"data": {"fn": John, "ln": Doe}
}