我正在使用带有cookie解析器的express并尝试使用cookie解析器保存cookie,我只能将它们保存在app.get中,但不能保存在app.post中。
可以将cookie保存在此处:
router.get('/login', function(req,res){
res.sendFile('/Users/samkirkiles/Desktop/AWSDemo/views/login.html');
//**** These cookies do save
res.cookie('username',"username")
res.cookie('password',"password")
//****
});
当我尝试在router.post中调用相同的代码res.cookie('用户名'," uesrname")时,一切都会运行,但不会保存Cookie。
router.post('/login', function(req,res,next){
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'users'
});
var username = req.body.username
var password = req.body.password
connection.query('SELECT username,password FROM accounts WHERE username=? AND password=?;', [username,password], function (error, results, fields) {
if (error) throw error;
if(results.length >= 1){
console.log("Login successful with given username to VerifyUserLoggedIn: " + username)
// ***** These cookies do not save
res.cookie('username',username)
res.cookie('password',password)
//***********
}else{
console.log("Login unsuccessful with given username to VerifyUserLoggedIn: " + username)
}
});
});
有谁知道为什么cookie不会保存在router.post中,但它们会保存在router.get中?