gpt4 book ai didi

node.js - 错误: cannot set up sessions without a secret or encryptionKey/signatureKey pair

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:19 25 4
gpt4 key购买 nike

我正在尝试让客户端 session 在 Ubuntu 上运行。但是,每当我运行 nodejs app.js 时,我都会收到此错误。我试图弄清楚发生了什么事,但我找不到发生了什么。我在 NPM/Github 网站上阅读了客户端 session 信息,但我不知道发生了什么。谁能帮助我或引导我到正确的地方?

整个错误:

/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)

最佳答案

要使用client-sessions,您必须设置secret或同时设置encryptionKeysignatureKey,如recommended in the documentation

https://www.npmjs.com/package/client-sessions#usage

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

code of lib/client-sessions.js检查 secret 或两个 key 是否在 clientSessionFactory 方法中初始化:

https://github.com/mozilla/node-client-sessions/blob/d0c20af3b0ed7750c68d3ae67819dfe203fa3d60/lib/client-sessions.js#L542

  if (!(opts.secret || (opts.encryptionKey && opts.signatureKey))) {
throw new Error("cannot set up sessions without a secret "+
"or encryptionKey/signatureKey pair");
}

https://hacks.mozilla.org/2012/12/using-secure-client-side-sessions-to-build-simple-and-scalable-node-js-applications-a-node-js-holiday-season-part-3/页面介绍了如何设置 secret - 通过使用一些长随机字符串(for example, combine several strings from the site random.org):

app.use(clientSessions({
secret: '0GBlJZ9EKBt2Zbi2flRPvztczCewBxXK' // set this to a long random string!
}));

关于node.js - 错误: cannot set up sessions without a secret or encryptionKey/signatureKey pair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49781364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com