简体   繁体   English

使用node.js客户端会话而不使用express

[英]Using node.js client-sessions without express

I'm trying to get client-sessions to work without using express but I'm not sure if I'm porting the example correctly. 我试图让客户端会话不使用快递而工作,但我不确定我是否正确移植了示例。

var sessionOptions = { cookieName: 'mySession', secret: 'blargadeeblargblarg', duration: 24 * 60 * 60 * 1000, activeDuration: 1000 * 60 * 5 }; var session = new SESSION(request, response, {}, sessionOptions);

When I run this client-sessions complains 当我运行这个客户端会话时抱怨

cannot set up sessions without a secret or encryptionKey/signatureKey pair 如果没有secret或encryptionKey / signatureKey对,则无法设置会话

Does client-sessions need express to work? 客户会话需要表达才能工作吗?

From https://github.com/mozilla/node-client-sessions : 来自https://github.com/mozilla/node-client-sessions

client-sessions is connect middleware 客户端会话是连接中间件

So while it might not need express , it needs connect to work as per the docs. 因此,虽然它可能不需要express ,但它需要按照文档connect到工作。

The specific error though, is because you aren't using the library correctly. 但是,具体的错误是因为您没有正确使用库。 You need to configure a session before using it. 您需要在使用之前配置会话。

var sessions = require("client-sessions");

var session = sessions({
  cookieName: 'mySession',
  secret: 'blargadeeblargblarg', 
  duration: 24 * 60 * 60 * 1000, 
  activeDuration: 1000 * 60 * 5
});

// then inside route handler..
session(req, res, function(){ console.log('done!'); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM