NodeJs和ExpressJs无法设置Cookie [英] NodeJs and ExpressJs Cannot Set Cookie
问题描述
您能帮我创建cookie,因为我不能使它工作。我想在用户登录后设置和创建cookie,但我不知道我的代码有什么问题。
Can you help me for creating cookies cause i can't make it work. I would like to set and create cookies after the user logs in but I don't know what's wrong with my codes. 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
//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);
推荐答案
要使用Cookie, c $ c> cookieSession 中间件。文档位于: http://expressjs.com/api.html#cookieSession
To use cookies you should look at the Express cookieSession
middleware. The docs are here: http://expressjs.com/api.html#cookieSession
更新(我现在无法测试,所以这是我记得的):
Update (I can't test it right now so this is from what I can remember):
您可以添加cookieSession中间件,并指定您的cookie的设置,如:
You can add the cookieSession middleware and specify the settings for your cookie with something like:
app.use(express.cookieSession({
cookie: {
path: '/',
maxAge: 3600000
}
}));
然后当您的用户登录时,您可以在会话上设置登录令牌 req.session.login_token ='login_token';
。
Then when your user logs in you can set the login token on the session with req.session.login_token = 'login_token';
.
当用户注销时,可以通过 req.session = null;
And when your user logs out you can clear the session by doing 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.
这篇关于NodeJs和ExpressJs无法设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!