[英]NodeJs and ExpressJs Cannot Set Cookie
Can you help me for creating cookies cause i can't make it work. 您能帮我创建Cookie吗,因为我无法使其正常工作。 I would like to set and create cookies after the user logs in but I don't know what's wrong with my codes.
我想在用户登录后设置和创建cookie,但是我不知道我的代码有什么问题。 Thanks Guys.
多谢你们。
Here's my code, if you think there's other error's or code correction can you help me fix it? 这是我的代码,如果您认为还有其他错误或代码更正,可以帮我解决它吗? Thanks guys.
多谢你们。 :)
:)
app.js app.js
//deps
var express = require('express');
var app = express();
var redis = require('redis');
var client = redis.createClient();
var delcookie = function(req, res) { res.clearCookie('login_token'); res.redirect('/'); };
var setcookie = function(req, res) { res.cookie('login_token', +new Date(), { maxAge: 3600000, path: '/' }); res.redirect('/'); };
//configs
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(app.router);
client.on('error', function (err) {
console.log('Error: ' + client.host + ':' + client.port + ' - ' + err);
});
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/login/', function (req, res) {
res.sendfile(__dirname + '/login.html');
});
app.get('/register/', function (req, res) {
res.sendfile(__dirname + '/register.html');
});
app.get('/restricted/', function (req, res) {
res.sendfile(__dirname + '/restricted.html');
});
app.get('/logout/', delcookie, function (req, res) {
res.sendfile(__dirname + '/logout.html');
});
//post
app.post('/login/', function (req, res) {
//res.sendfile(__dirname + '/restricted.html');
client.hmget( req.body.user.username, 'password', function (err,pass) {
if ( (!err) && pass && pass == req.body.user.password ){
res.redirect('/restricted/', setcookie);
} else if ( pass == false) {
res.redirect('/register/');
} else {
res.redirect('/login/');
}
});
});
app.post('/register/', function (req, res) {
client.hmset(req.body.user.username, 'password',req.body.user.password,
'fname',req.body.user.fname, 'lname', req.body.user.lname,
'password', req.body.user.password, 'email', req.body.user.email,
'mobile', req.body.user.mobile, redis.print);
res.write('Successfully Registered');
});
app.listen(80);
To use cookies you should look at the Express cookieSession
middleware. 要使用cookie,您应该查看Express
cookieSession
中间件。 The docs are here: http://expressjs.com/api.html#cookieSession 这些文档在这里: http : //expressjs.com/api.html#cookieSession
Update (I can't test it right now so this is from what I can remember): 更新 (我现在无法对其进行测试,因此这是我所记得的):
You can add the cookieSession middleware and specify the settings for your cookie with something like: 您可以添加cookieSession中间件,并使用以下方式指定cookie的设置:
app.use(express.cookieSession({
cookie: {
path: '/',
maxAge: 3600000
}
}));
Then when your user logs in you can set the login token on the session with req.session.login_token = 'login_token';
然后,当用户登录时,您可以使用
req.session.login_token = 'login_token';
在会话上设置登录令牌req.session.login_token = 'login_token';
. 。
And when your user logs out you can clear the session by doing req.session = null;
当用户注销时,您可以通过执行
req.session = null;
清除会话req.session = null;
. 。
In your authentication middleware you can then check that the login token is set on the session to see if the user is authenticated. 然后,在身份验证中间件中,您可以检查是否在会话中设置了登录令牌,以查看用户是否已通过身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.