简体   繁体   English

NodeJ和ExpressJ无法设置Cookie

[英]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.

相关问题 ExpressJS和Cookie-无法获取express-cookie来初始化Cookie - ExpressJS & Cookies - Cannot get express-cookie to Initialize a Cookie 无法使用jQuery Cookie设置Cookie - Cannot set cookie with jQuery Cookie 使用 JWT 设置 cookie - NodeJs 和 Javascript - Set cookie with JWT - NodeJs and Javascript Cookie 未在 NodeJS 和 NextJS 的请求中设置 - Cookie not set in request with NodeJS and NextJS 我无法在 expressjs 的 http post 请求中设置服务器端 cookie - i can not set server side cookie in http post request in expressjs 为什么expressjs会在每个OPTIONS响应中发送Set-Cookie标头? - Why is expressjs sending Set-Cookie header with every OPTIONS response? express.js TypeError:无法设置未定义的属性“会话” - expressjs TypeError: Cannot set property 'session' of undefined 使用express.js将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client with expressjs (Nodejs, expressjs, mongodb) UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“用户名” - (Nodejs, expressjs, mongodb) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined 提交表单时出错:无法使用 NodeJS + ExpressJS 发布 - Error when submitting form: Cannot POST using NodeJS + ExpressJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM