节点、会话存储删除过期会话
我正在尝试为express.js 节点应用程序实现会话存储 我的问题是:
- 如何删除具有浏览器会话生存期的 cookie(根据连接文档标记有 expires = false)
- 我应该将会话数据存储为 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 :
- How do you delete cookie that have a browser session lifetime (marked with expires = false according to connect doc)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 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.