2017-10-11 12 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を取得する定義されていません。私は何かが欠けていますか? – venkatesh

+0

userData.idが私のダミーのキーであるキー@Vishalをここで設定する必要があります。あなた自身のキーと値を設定してください。 –

+0

はコードを取得できません。それの使い方。私はgithubに自分のコードを投稿する必要があります – venkatesh

関連する問題