Node.js 错误:无法设置没有密钥或encryptionKey/signatureKey对的会话
我正在尝试让客户端会话在Ubuntu上工作。但是,每当我运行Node.js 错误:无法设置没有密钥或encryptionKey/signatureKey对的会话,node.js,ubuntu,Node.js,Ubuntu,我正在尝试让客户端会话在Ubuntu上工作。但是,每当我运行nodejs app.js时,就会出现这个错误。我正试图弄清楚发生了什么,但我找不到发生了什么。我在他们的NPM/Github站点上阅读了客户端会话信息,但我不知道发生了什么。有人能帮我或带我到正确的地方吗 整个错误: /home/tom/cookiestut/node_modules/client-sessions/lib/client-sessions.js:548 throw new Error("cannot set u
nodejs app.js
时,就会出现这个错误。我正试图弄清楚发生了什么,但我找不到发生了什么。我在他们的NPM/Github站点上阅读了客户端会话信息,但我不知道发生了什么。有人能帮我或带我到正确的地方吗
整个错误:
/home/tom/cookiestut/node_modules/client-sessions/lib/client-sessions.js:548
throw new Error("cannot set up sessions without a secret "+
^
Error: cannot set up sessions without a secret or encryptionKey/signatureKey pair
at clientSessionFactory (/home/tom/cookiestut/node_modules/client-sessions/lib/client-sessions.js:548:11)
at Object.<anonymous> (/home/tom/cookiestut/app.js:34:9)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
/home/tom/cookiestut/node\u modules/client sessions/lib/client sessions.js:548
抛出新错误(“无法设置没有密码的会话”+
^
错误:无法设置没有密钥或encryptionKey/signatureKey对的会话
在clientSessionFactory(/home/tom/cookiestut/node_modules/client sessions/lib/client sessions.js:548:11)
在对象上。(/home/tom/cookiestut/app.js:34:9)
at模块编译(内部/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:665:10)
在Module.load(内部/modules/cjs/loader.js:566:32)
在tryModuleLoad(内部/modules/cjs/loader.js:506:12)
at Function.Module._load(内部/modules/cjs/loader.js:498:3)
位于Function.Module.runMain(internal/modules/cjs/loader.js:695:10)
启动时(内部/bootstrap/node.js:201:19)
在bootstrapNodeJSCore(internal/bootstrap/node.js:516:3)
要使用客户端会话
必须设置机密
或同时设置加密密钥
和签名密钥
,如下所示
页面说明了如何使用一些长随机字符串()设置密码
:
要使用
客户端会话
,必须将机密
或加密密钥
和签名密钥
设置为
页面说明了如何使用一些长随机字符串()设置密码
:
我认为您得到的错误是不言自明的。如果您遵循堆栈跟踪,您可以在以下路径和行中看到:
/home/tom/cookiestut/app.js:34:9
您没有设置cookie会话配置的秘密。可能您已将其配置为环境变量,但忘记了定义它
在cookie设置中手动添加一个秘密我认为您得到的错误是不言自明的。如果您遵循堆栈跟踪,您可以在以下路径和行中看到:
/home/tom/cookiestut/app.js:34:9
您没有设置cookie会话配置的秘密。可能您已将其配置为环境变量,但忘记了定义它
在cookie设置中手动添加一个秘密查看此处:请添加一些应用程序代码:如何导入库,初始化它。将会更好查看此处:请添加一些应用程序代码:如何导入库,初始化它。将会更好
var sessions = require("client-sessions");
app.use(sessions({
cookieName: 'mySession', // cookie name dictates the key name added to the request object
secret: 'blargadeeblargblarg', // should be a large unguessable string
duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
}));
app.use(function(req, res, next) {
if (req.mySession.seenyou) {
res.setHeader('X-Seen-You', 'true');
} else {
// setting a property will automatically cause a Set-Cookie response
// to be sent
req.mySession.seenyou = true;
res.setHeader('X-Seen-You', 'false');
}
});
if (!(opts.secret || (opts.encryptionKey && opts.signatureKey))) {
throw new Error("cannot set up sessions without a secret "+
"or encryptionKey/signatureKey pair");
}
app.use(clientSessions({
secret: '0GBlJZ9EKBt2Zbi2flRPvztczCewBxXK' // set this to a long random string!
}));