我正在尝试使用 express 框架设置 node.js 服务器并实现会话。
这有效:
var express = require('express');
var session = require('express-session');
var redis = require('redis');
var server = express();
var redisClient = redis.createClient();
server.use(session({secret: 'mySuperSecretKey'}));
server.listen(8000, function () {
console.log('listening...');
});
server.get('/', function (req, res) {
req.session.aSessionEntry = 'value';
res.send('Your session entry is' + req.session.aSessionEntry);
});
但是,当我在异步函数中声明 server.use(session(...)) 时,它会被忽略 - 会话处理不再起作用。
这里 :
var express = require('express');
var session = require('express-session');
var redis = require('redis');
var server = express();
var redisClient = redis.createClient();
redisClient.get("sessionSecret", function(err, reply) {
server.use(session({secret: reply.toString()}));
};
server.listen(8000, function () {
console.log('listening...');
});
server.get('/', function (req, res) {
req.session.aSessionEntry = 'value';
res.send('Your session entry is' + req.session.aSessionEntry);
});
为什么不能这样做?