node.JS - Express 框架创建会话并检查会话是否存在
这是我的服务器代码,请看一下代码中的注释。
var app = express.createServer();
app.get('/', function(req, res){
// create a session with the value of an email for example: [email protected]
});
// down here I want to check the session if the session exist
if( there is a session with value [email protected]){
//do stuff
}
This is my server code, please take a look at the comments in the code.
var app = express.createServer();
app.get('/', function(req, res){
// create a session with the value of an email for example: [email protected]
});
// down here I want to check the session if the session exist
if( there is a session with value [email protected]){
//do stuff
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Connect / Express 中的会话实现方式不允许自定义会话 ID。部分原因是出于安全考虑。所以你可以做很多事情。
将所有内容存储在会话中。在某处(可能在数据库中)创建一个索引,将电子邮件地址映射到会话 ID,以便您可以通过电子邮件查找会话。
仅在会话中存储电子邮件。将实际会话数据保存在其他地方(可能在数据库中),您可以通过电子邮件地址获取它。
创建您自己的会话中间件,可能基于 Connect 的代码。 Connect 中的实际会话代码只有 300 行。但要非常小心,保持安全功能完好无损。
The way sessions are implemented in Connect / Express doesn't allow for a custom session ID. This is partly because of security. So there's a bunch of things you can do.
Store everything in the session. Create an index somewhere (perhaps in a database) that maps email addresses to session IDs, so you can look up sessions by email.
Store only the email in the session. Keep the actual session data elsewhere (perhaps in a database), where you can fetch it by email address.
Create your own session middleware, perhaps based off Connect's code. The actual session code in Connect is a mere 300 lines. But be very careful to keep security features intact.