There are multiple ways to write a conditional statement in JS. However, if there are multiple statements I would say you should stick to if else if else. But if you want to see other approaches, here they are:
Using Ternary operator ? :
const {pr} = req.query
pr === 'trans'
? util.transUrl(req.originalUrl).then(param =>
res.redirect(param)
)
: pr === 'inst'
? util.instaUrl(req.originalUrl).then(param =>
res.redirect(param)
)
: res.status(400).send('Contact the editor of the originating page.')
Using Gate logic && ||
const {pr} = req.query
(pr === 'trans' &&
util.transUrl(req.originalUrl).then(param =>
res.redirect(param))
) ||
(pr=== 'inst' &&
util.instaUrl(req.originalUrl).then(param =>
res.redirect(param))
) ||
res.status(400).send('Contact the editor of the originating page.')
Now, Looking at your code, here if and else if statements are similar. So you can avoid else if using ternary operator like this:
const {pr} = req.query
if(pr === 'trans' || pr === 'inst'){
util[pr === 'trans' ? 'transUrl' : 'instaUrl'](req.originalUrl)
.then(param => res.redirect(param))
}
else{
res.status(400).send('Contact the editor of the originating page.')
}
Just one FYI: Please consider using === instead of == whenever you are comparing strings and there is no need of coercion.
ifandelse ifcould be condensed, but I prefer the current version, it's more readable. To make things a bit more DRY you could define a functionparam => res.redirect(param)