使用没有Express的node.js客户端会话 [英] Using node.js client-sessions without express

查看:45
本文介绍了使用没有Express的node.js客户端会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不使用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',秘密:"blargadeeblargblarg",持续时间: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

客户会话需要表达才能正常工作吗?

Does client-sessions need express to work?

推荐答案

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

client-sessions是连接中间件

client-sessions is connect middleware

因此,尽管可能不需要 express ,但它需要 connect 才能按照文档工作.

So while it might not need express, it needs connect to work as per the docs.

但是,特定的错误是因为您没有正确使用库.您需要先配置会话,然后再使用它.

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!'); });

这篇关于使用没有Express的node.js客户端会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆