In my node app I use 4 routes to get data and asynchronously when the data is received, I'm setting the session. I use express.js and expression-session npm. But setting the session works time to time. (sporadically setting the data) . Sometimes all the data is no setting. Please help me out.
Important Code snippets
var session = require('express-session');
app.use(session({ secret: 'testQWERTY',cookie: { maxAge: 3600000 }}));
app.get('/route 1', function(req, res , next) {
var apiPortalURL = "testUrl";
request({
url: apiPortalURL,
qs: {},
json: req.body,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}, function(error, response, body){
if(error) {
console.log("error "+error);
res.sendStatus(error);
} else {
req.session.data1 = response.body;
res.status(response.statusCode);
res.send(response.body);
}
});
});
app.get('/route 2', function(req, res , next) {
//same as 1
req.session.data2 = response.body;
});
app.get('/route 3', function(req, res , next) {
//same as 1
req.session.data3 = response.body;
});
app.get('/route 4', function(req, res , next) {
//same as 1
req.session.data4 = response.body;
});