-1

i have a route like this

router.post('/policy', async (req, res, next) => {
    try {
        console.log(req.body);
    } catch (e) {
        console.log(e)
        res.sendStatus(500)
    }
})

I want the same logic to be executed when /role is called what is the correct way to do this ? ( preferrably without copy pasting the same code )

I can think of this logic

router.post('/policy', async (req, res, next) => {
    try {
        console.log(req.body);
    } catch (e) {
        console.log(e)
        res.sendStatus(500)
    }
})

router.post('/role', async (req, res, next) => {
    try {
        console.log(req.body);
    } catch (e) {
        console.log(e)
        res.sendStatus(500)
    }
})

Why am i naming the routes differently for same logic ?

Because i want the back-end to be more read-able and specific to what task it is doing i.e adding policy OR role

Phil
  • 435
  • 2
  • 9
  • 28

1 Answers1

2

This will work for both route /policy and /role:

router.post(['/policy', '/role'], async (req, res, next) => {
    try {
        console.log(req.body);
    } catch (e) {
        console.log(e)
        res.sendStatus(500)
    }
})
aRvi
  • 2,203
  • 1
  • 14
  • 30