2015-12-02 109 views
0

我試圖讓會話使用節點。我使用Express 4.8.7。我遵循教程here節點Express會話未被保存

我已經建立了在8080端口上運行我有一個角應用在端口8081

運行在我server.js文件的頂部,我在節點中的API:

var session = require('express-session'); 
var bodyParser = require('body-parser'); 

app.use(session({ 
    secret: 'ssshhhhh' 
})); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

var sess; 

當我登錄,我做的:

app.post('/login', function(req, res) { 

    var successCallback = function(res, status, content) { 

     sess = req.session; 
     sess.email = req.body.email; 

     console.log('in LOGIN and req.session is ', req.session); 

     sendJsonResponse(res, status, content); 
    } 

    ctrlUsers.signin(req, res, successCallback); 

}); 

並登錄後,前端做請求到/文件端點:

app.get('/files', function(req, res){ 

    console.log('in files route and req.session is ', req.session); 

}); 

在我已經鏈接到上面的示例中,req.session應該包含一個包含電子郵件地址的屬性。但它沒有,它只是包含:

{ cookie: 
    { path: '/', 
    _expires: null, 
    originalMaxAge: null, 
    httpOnly: true } } 

任何想法爲什麼會話沒有得到正確保存?

回答

0

變化

app.get('/files', function(req, res){ 

    console.log('in files route and req.session is ', req.session); 

}); 

到==>

var usersession = req.session; 
app.get('/files', function(req, res){ 

    console.log('in files route and req.session is ', usersession); 

});