Beren
Beren

Reputation: 744

Is there any way to nest/ call a route inside another route in node JS and express JS?

I want to automatically execute second route after the first route executed. For example: I have my first route

router.post('/signup', async (req, res) => {
*****Code*****
}

and my second route

router.post('/signupLog', async (req, res) => {
*****Code*****
}

So i want to call my second route automatically just after the first route because the second route will take some values from first route and calculate them and have some new fields in it to fill with the calculations from first route.

Upvotes: 0

Views: 71

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30995

Why don't extract the second route as function and call it ?

const secondRoute = async (req, res) => {
     *****Code*****
}

router.post('/signup', async (req, res) => {
      *****Code*****
   await secondRoute(req, res);
      *****Code*****
}

router.post('/signupLog', secondRoute)

Upvotes: 1

Related Questions