我正在开发网站并使用nodejs(express)+ postgresql。 我想为所有文件使用会话值。我添加了我的代码。
app.js
var session = require('express-session');
var routes = require('./routes/index');
var app = express();
require('./conversation/conversation')(app);
//路由/ index.js
var sess;
router.post('/login, function(req, res) {
sess = req.session;
sess.userId = 8;
});
router.get('/test1', function(req, res) {
sess = req.session;
var userId = sess.userId;
console.log(userId);
// output 8
});
//会话/ conversation.js
router.get('/test2', function(req, res) {
sess = req.session;
var userId = sess.userId;
console.log(userId);
// get userId is undefined
});
答案 0 :(得分:0)
app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));