Note that there are some explanatory texts on larger screens.

plurals
  1. POnode, session store remove expired sessions
    primarykey
    data
    text
    <p>I am trying to implement a session store for an express.js node app My question are :</p> <ol> <li>How do you delete cookie that have a browser session lifetime (marked with expires = false according to connect doc)</li> <li>Should I store session data as a json string or directly as an object</li> </ol> <p>this is the coffee script I came up with so far, using mongoose as I this is the orm I chose for the app</p> <pre><code> 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) -&gt; 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 = =&gt; @Session.remove expires: { '$lte': new Date() } setInterval removeExpires, options.interval get: (sid, fn) -&gt; @Session.findOne sid: sid, (err, session) -&gt; if session? try fn null, JSON.parse session.data catch err fn err else fn err, session set: (sid, data, fn) -&gt; 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) -&gt; @Session.remove { sid: sid }, fn all: (fn) -&gt; @Session.find { expires: { '$gte': new Date() } }, [ 'sid' ], (err, sessions) -&gt; if sessions? fn null, (session.sid for session in sessions) else fn err clear: (fn) -&gt; @Session.drop fn length: (fn) -&gt; @Session.count {}, fn </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload