Now.js 中的会话支持

发布于 2024-12-29 13:31:22 字数 503 浏览 0 评论 0原文

Now.js 引用:

只需在 nowjs.initialize 中传递一个连接或快速 http 服务器, this.user.session 就应该可用。

所以:

express = require 'express'
app = module.exports = express.createServer()
connect = require 'connect'
nowjs = require 'now'
everyone = nowjs.initialize(app)

this.user 的输出是:

{ 
  clientId: '353725111301231610',
  cookie: { 'connect.sid': 's0meC00k1e1nF0rm4ti0n' },
  session: undefined 
}

知道为什么会话未定义吗?

Now.js quotes:

Simply pass a connect or express http server in nowjs.initialize and this.user.session should be available.

So:

express = require 'express'
app = module.exports = express.createServer()
connect = require 'connect'
nowjs = require 'now'
everyone = nowjs.initialize(app)

The output of this.user is:

{ 
  clientId: '353725111301231610',
  cookie: { 'connect.sid': 's0meC00k1e1nF0rm4ti0n' },
  session: undefined 
}

Any idea why session is undefined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟若柳尘 2025-01-05 13:31:22

我遇到了这个问题,结果发现它的发生是因为我在配置 Express 之前初始化了 NowJS。

app.configure(function(){
    ...

    app.use(express.cookieParser());
    app.use(express.session({ secret: "my_key", store: redisStore }));
    app.use(app.router);
});

var everyone = nowjs.initialize(app); //must be defined after app.configure

需要注意的一件事是,在向服务器发出请求之前,不会设置会话,因此如果服务器重置,客户端套接字将重新连接,但在浏览器刷新页面之前,会话将处于未定义状态。

I ran into this problem and turns out it was happening because I was initializing NowJS before configuring express.

app.configure(function(){
    ...

    app.use(express.cookieParser());
    app.use(express.session({ secret: "my_key", store: redisStore }));
    app.use(app.router);
});

var everyone = nowjs.initialize(app); //must be defined after app.configure

One thing to note is that session is not set until a request is made to the server so if the server is reset, the client socket will reconnect but session will be undefined until the browser does a page refresh.

雪若未夕 2025-01-05 13:31:22

我设法为每个“每个人”请求找到一个解决方案(CoffeeScript):

self = this
sid = decodeURIComponent(this.user.cookie['connect.sid'])
sessionStore.get sid, (err, session) ->
    self.user.session = session
    <<more code here>>

有什么方法可以直接访问 this.user.session?

I managed to find a solution (CoffeeScript), for every "everyone"-request:

self = this
sid = decodeURIComponent(this.user.cookie['connect.sid'])
sessionStore.get sid, (err, session) ->
    self.user.session = session
    <<more code here>>

Any way to get to this.user.session directly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文