节点、会话存储删除过期会话

发布于 2024-12-05 19:45:12 字数 2135 浏览 2 评论 0原文

我正在尝试为express.js 节点应用程序实现会话存储 我的问题是:

  1. 如何删除具有浏览器会话生存期的 cookie(根据连接文档标记有 expires = false)
  2. 我应该将会话数据存储为 json 字符串还是直接存储为对象,

这是我想出的咖啡脚本,所以到目前为止,我使用猫鼬,这是我为应用程序选择的 orm

    express  = require 'express'
    mongoose = require "mongoose"
    util     = require "util"

    # define session schema
    SessionSchema = new mongoose.Schema
      sid     : { type: String, required: true, unique: true }
      data    : { type: String, default: '{}' }
      expires : { type: Date, index: true }

    module.exports =
    class SessionStore extends express.session.Store
      constructor: (options) ->

        console.log "creating new session store"

        options ?= {}

        # refresh interval
        options.interval ?= 60000 

        options.url ?= "mongodb://localhost/session" 

        # create dedicated session connection
        connection = mongoose.createConnection options.url

        # create session model
        @Session = connection.model 'Session', SessionSchema

        # remove expired session every cycles
        removeExpires = => @Session.remove expires: { '$lte': new Date() }

        setInterval removeExpires, options.interval

      get: (sid, fn) ->
        @Session.findOne sid: sid, (err, session) ->
          if session?
            try
              fn null, JSON.parse session.data
            catch err
              fn err
          else
              fn err, session

      set: (sid, data, fn) ->

        doc =
            sid: sid
            data: JSON.stringify data
            expires:  data.cookie.expires 
        try
          @Session.update sid: sid, doc, upsert: true, fn
        catch err
          fn err

      destroy: (sid, fn) ->
        @Session.remove { sid: sid }, fn

      all: (fn) ->
        @Session.find { expires: { '$gte': new Date() } }, [ 'sid' ], (err, sessions) ->
          if sessions?
            fn null, (session.sid for session in sessions)
          else
            fn err

      clear: (fn) -> @Session.drop fn

      length: (fn) -> @Session.count {}, fn

I am trying to implement a session store for an express.js node app
My question are :

  1. How do you delete cookie that have a browser session lifetime (marked with expires = false according to connect doc)
  2. Should I store session data as a json string or directly as an object

this is the coffee script I came up with so far, using mongoose as I this is the orm I chose for the app

    express  = require 'express'
    mongoose = require "mongoose"
    util     = require "util"

    # define session schema
    SessionSchema = new mongoose.Schema
      sid     : { type: String, required: true, unique: true }
      data    : { type: String, default: '{}' }
      expires : { type: Date, index: true }

    module.exports =
    class SessionStore extends express.session.Store
      constructor: (options) ->

        console.log "creating new session store"

        options ?= {}

        # refresh interval
        options.interval ?= 60000 

        options.url ?= "mongodb://localhost/session" 

        # create dedicated session connection
        connection = mongoose.createConnection options.url

        # create session model
        @Session = connection.model 'Session', SessionSchema

        # remove expired session every cycles
        removeExpires = => @Session.remove expires: { '$lte': new Date() }

        setInterval removeExpires, options.interval

      get: (sid, fn) ->
        @Session.findOne sid: sid, (err, session) ->
          if session?
            try
              fn null, JSON.parse session.data
            catch err
              fn err
          else
              fn err, session

      set: (sid, data, fn) ->

        doc =
            sid: sid
            data: JSON.stringify data
            expires:  data.cookie.expires 
        try
          @Session.update sid: sid, doc, upsert: true, fn
        catch err
          fn err

      destroy: (sid, fn) ->
        @Session.remove { sid: sid }, fn

      all: (fn) ->
        @Session.find { expires: { '$gte': new Date() } }, [ 'sid' ], (err, sessions) ->
          if sessions?
            fn null, (session.sid for session in sessions)
          else
            fn err

      clear: (fn) -> @Session.drop fn

      length: (fn) -> @Session.count {}, fn

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

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

发布评论

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

评论(1

迷爱 2024-12-12 19:45:12

我对 Node 还很陌生,所以对此持保留态度。

虽然不直接面向会话,但记住我的 dailyjs 教程将帮助我想有点。特别是他验证登录令牌的最后一段代码。

另外,我认为最好解析 JSON 并将其存储为对象。如果您预先进行解析,应该更容易以这种方式访问​​不同的 cookie 位。

I'm pretty new to node so take this with a grain of salt.

While not directly session oriented the remember me tutorial on dailyjs will help a bit I think. Specifically the last bit of code where he verifies login tokens.

Also, I believe it's better to parse the JSON and store as an object. Should be easier to get access to the different cookie bits that way if you take care of the parse up front.

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