DEV Community

Mayur Aitavadekar
Mayur Aitavadekar

Posted on

 

Where to store some important data fetched from API so that I can use it in multiple routes?

I am developing OTP authentication. So here my routes in express -

const express = require("express");
const router = require("express").Router();
const rp = require("request-response");

router.post("/sendOTP", (req, res) => {

const { mobile_number }  = req.body; // receive the mobile number

const otp = randomize("0000"); // generated 4 digit otp

options = {
     method: "GET", 
     url: "xxxxx/xxxxx/xxxxx/${otp}",   // calling my otp service provider to send otp to my user
}

rp(options).then((response)=>{

       // response.details contains the unique otp session id which I have to use in another route where I am verifying otp
}).catch((err) => console.log(err));
});

router.post("/getOTP", (req, res) => {

// user will put otp he received from his mobile

const { otp }  = req.body; // receive the otp

options = {
     method: "GET", 
     url: "xxxxx/xxxxx/xxxxx/${otp}/${otp_session_id}",   // calling my otp service provider with otp and session id of otp
}

rp(options).then((response)=>{
        console.log(response) // otp verified.

}).catch((err) => console.log(err));
});

this is simple code. Now my problem is - how can I store that otp_session_id so that I can use it in two routes sendOTP and getOTP ? I used cookieParser, cookieSession, expressSession, nothing worked out. please any simple steps / solutions will be great.

thanks :)

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.