7
無法在請求中設置Cookie。ExpressJS設置/獲取/使用Cookie
設置我與
response.cookie('name', 'My name');
我想獲得我的餅乾這樣的cookie,而它之前的工作,但我改變了快速配置,我不知道是什麼,現在似乎是這個問題。
request.cookies is and empty Object
我快速配置:
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());
});
設置的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"};
});
};
獲取的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?
};
你在哪裏調用''singIn'「? –
我認爲你需要使用cookieSession中間件。請參閱http://stackoverflow.com/a/18510185/446681 –
singIn在用戶使用主密碼和密碼進入時調用。在獲取用戶的所有數據時使用帳戶。 – puppeteer701