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

Issue

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.

Solution

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)

Answered By – BENARD Patrick

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published