koshur
koshur

Reputation: 134

How to add json data to redirect in Node Js?

this route takes information json

router.get('/sendOtpforMobileVerification', async (req, res) => {
    console.log("\n//Registration PID 9".bold)
    console.log("\nVOID API (PID 9) : Sending Verification Email".cyan.bold);


    const userIdForOtpVerification = req.body.id;
    const userPhoneForOtpVerification = req.body.mobile;

    ...bla bla bla

});

The above URL is working as expected. what I am trying to do is perform its action from another route

//some other route

router.get('/change', async (req, res) => {
res.redirect('/void/api/authentication/sendVerificationMail');
-here i want to call the above route with json data
});

Upvotes: 0

Views: 390

Answers (2)

Deniz Babat
Deniz Babat

Reputation: 316

Router functions is independ functions among their. You can create a global value(class, static value etc.) and you send a request from client to server for first get function and thus you store the data and then you can send a new request from client to server, thus you can use the stored data.

Upvotes: 1

vazsonyidl
vazsonyidl

Reputation: 471

I think you can pass query params to the redirect route, which are accessible from the query parameters, as follows:

const url = require('url');   

res.redirect(url.format({
       pathname:"/anythingyouwant",
       query: {
          "something":"your infos here"
        }
     })); 

Then you can get access to the passed query with the following:

app.get('/anythingyouwant', function(req, res) {
  var passedVariable = req.query.something;
});

Upvotes: 1

Related Questions