javascript ExpressJS 设置/获取/使用 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19591689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExpressJS set/get/use cookies
提问by puppeteer701
Cannot get setted cookies within requests.
无法在请求中设置 cookie。
I set my cookie with
我设置了我的 cookie
response.cookie('name', 'My name');
I would like to get my cookie this way, and it worked before, but I changed express configuration, and I don't know what seems to be the problem now.
我想通过这种方式获取我的 cookie,并且它以前工作过,但是我更改了 express 配置,我不知道现在似乎是什么问题。
request.cookies is and empty Object
My express configuration:
我的快递配置:
var express = require('express'),
api = require('./routes/api');
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
//app.use(express.bodyParser());
app.use(express.compress()); // New call to compress content
app.use(express.cookieParser());
app.use(express.session({secret: 'secret'}));
app.use(app.router);
app.use(express.methodOverride());
//app.use(express.static(__dirname + '/public'));
});
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
next();
});
app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
app.use(express.errorHandler());
});
Setting Cookie:
设置饼干:
exports.singIn = function (req, res) {
var mail = req.query.mail,
password = req.query.password;
return user.find({
mail: mail
}).then(function (d) {
var user = usersData(u);
res.cookie('mail', user.mail, { maxAge: 900000});
res.cookie('password', crypted, { maxAge: 900000});
res.json({ user: user });
return { user: user }
}).catch(function () {
res.json(400, {"error-tag": "error-sing-in"});
return {"error-tag": "error-sing-in"};
});
};
Getting Cookie:
获取 Cookie:
exports.account = function (req, res) {
var mail = req.cookies.mail,
password = req.cookies.password;
//here req.cookies is an empty object. I don't know why?
};
回答by danday74
An answer for 2017 ..
2017年的答案..
const cookieParser = require('cookie-parser');
const express = require('express');
const app = express();
app.use(cookieParser());
to get a cookie from an incoming request ..
从传入的请求中获取 cookie ..
let cookie = req.cookies['cookiename'];
to set a response cookie (this cookie will be sent on all future incoming requests until deletion or expiration) ..
设置响应 cookie(此 cookie 将在所有未来传入请求中发送,直到删除或到期)..
res.cookie('cookiename', 'cookievalue', {
maxAge: 86400 * 1000, // 24 hours
httpOnly: true, // http only, prevents JavaScript cookie access
secure: true // cookie must be sent over https / ssl
});
to delete a response cookie ..
删除响应 cookie ..
res.cookie('cookiename', 'cookievalue', {
maxAge: 0
});