2017-10-11 24 views
0

iam使用Mysql,ejs模板與angular tech。在這裏我使用session進行登錄,但沒有任何存儲。
這是app.js文件無法將會話存儲在expressejs(ejs)角度

var index = require('./routes/index'); 
var users = require('./routes/users'); 

app.set('view engine', 'ejs'); 
app.use(express.static('views')); 
app.use(express.static(__dirname + '/views/views')); 
app.use(cookieParser()); 
app.use(morgan('dev')); 
app.use(bodyParser.urlencoded({'extended': 'true'})); 
//app.use(session({secret: 'keyboard cat', key: 'sid'})); 

app.use(session({ 
    cookieName: 'session', 
    secret: 'random_string_goes_here', 
    duration: 30 * 60 * 1000, 
    activeDuration: 5 * 60 * 1000, 
})); 

app.use(bodyParser.json()); 
app.use(passport.initialize()); 
app.use(passport.session()); 
app.use(bodyParser.json({type: 'application/vnd.api+json'})); 
app.use(methodOverride()); 


這裏登錄表單後

app.post('/userlogins', function (req, res, next) { 
    var pass = sha1(req.body.password); 
    var email = req.body.email; 
    var password = pass; 
    connection.query('SELECT email,password FROM register where email = "' + email + '" and password = "' + password + '" ', function (err, rows) { 

     // console.log(query.sql); 
     if (err) { 
      throw err(); 
      var errornya = ("Error Selecting : %s ", err.code); 
      console.log(err.code); 
      req.flash('msg_error', errornya); 
      res.redirect('/login'); 
     } else { 
      // console.log('the record inserted:', result); 
      if (rows.length <= 0) 
      { 
       message: 'Invalid password' 
       res.redirect('/userlogins'); 
      } else { 
       if (rows.length <= 0) 
       { 
        message: 'Invalid password' 
        res.redirect('/userlogins'); 
       } else 
       { 
        //req.session.user = user; 
        console.log(req.session);  //getting empty value. 
        message: 'successfully logged In!' 
        res.redirect('/dashboard'); 
       } 
      } 
     } 
    }); 
    // console.log(query.sql); 
}); 
+0

?在你的應用程序@Vishal –

+0

是的,我會將他的電子郵件/用戶帶到儀表板。在這裏我也使用角度。你可以向我解釋我失蹤的地方嗎?爲什麼我無法進行會議? – venkatesh

回答

0

您可以在節點的應用程序使用一個小單張類,然後使用它來進行應用。

'use strict'; 
let nodeCache = require('node-cache'); // i am using this as i want the details to auto expire too . 

let myCache = new nodeCache({stdTTL: 86400, checkperiod: 120}); 

module.exports = { 
    get: (key) => myCache.get(key), 
    set: (key, value) => myCache.set(key, value) 
}; 

使用它像這樣

const cache = require('../cache'); 
cache.set(userData.id, userData); // dummy key and value 
cache.get(ctx.get('<your key>')); 
要存儲用戶上下文
+0

上面我創建了一個名爲cache.js的文件,並在app.js中調用。現在得到錯誤userData.id沒有定義。 Iam錯過了嗎? – venkatesh

+0

你需要設置密鑰@Vishal userData.id是我的虛擬密鑰,在這裏你設置你自己的密鑰和值 –

+0

我沒有得到代碼。如何使用它。我應該在github中發佈我的代碼 – venkatesh