Nods.js Session偶尔会有效

时间:2016-02-11 10:05:13

标签: node.js session express asynchronous express-session

在我的节点应用中,我使用 4条路线来获取数据,并在收到数据时异步,我设置会话。我使用 express.js expression-session npm 。但设置会话时不时。 (偶尔设置数据)。有时所有数据都没有设置。请帮帮我。

重要代码摘要

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;

});

0 个答案:

没有答案