New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

connect-mongodb-session

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-mongodb-session

MongoDB session store for connect built by MongoDB

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by11.04%
Maintainers
1
Weekly downloads
 
Created
Source

connect-mongodb-session

MongoDB-backed session storage for connect and Express. Meant to be a well-maintained and fully-featured replacement for modules like connect-mongo

MongoDBStore

This module exports a single function which takes an instance of connect (or Express) and returns a MongoDBStore class that can be used to store sessions in MongoDB.

It can store sessions for Express 4

If you pass in an instance of the express-session module the MongoDBStore class will enable you to store your Express sessions in MongoDB.

    
    var express = require('express');

    var MongoDBStore = connectMongoDB(require('express-session'));

    var app = express();
    var store = new MongoDBStore(
      { 
        uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
        collection: 'mySessions'
      },
      function(error) {
        assert.ifError(error);

        app.use(require('express-session')({
          secret: 'This is a secret',
          cookie: {
            maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
          },
          store: store
        }));

        app.get('/', function(req, res) {
          res.send('Hello ' + JSON.stringify(req.session));
        });

        var server = app.listen(3000);

        underlyingDb.collection('mySessions').count({}, function(error, count) {
          assert.ifError(error);
          assert.equal(0, count);

          request('http://localhost:3000', function(error, response, body) {
            assert.ifError(error);
            assert.equal(1, response.headers['set-cookie'].length);
            var cookie = require('cookie').parse(response.headers['set-cookie'][0]);
            assert.ok(cookie['connect.sid']);
            underlyingDb.collection('mySessions').count({}, function(error, count) {
              assert.ifError(error);
              assert.equal(1, count);
              server.close();
              done();
            });
          });
        });
      });
  
It can store sessions for latest Express 3.x

If you're using Express 3.x, you need to pass the Express module itself rather than the express-session module. Session storage is part of the Express core in 3.x but not in 4.x.

    
    var express = require('../vendor/express-3.18.1');

    var MongoDBStore = connectMongoDB(express);

    var app = express();
    var store = new MongoDBStore(
      {
        uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
        collection: 'mySessions'
      });

    app.use(express.session({
      secret: 'This is a secret',
      cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
      },
      store: store
    }));

    app.get('/', function(req, res) {
      res.send('Hello ' + JSON.stringify(req.session));
    });

    var server = app.listen(3000);

    underlyingDb.collection('mySessions').count({}, function(error, count) {
      assert.ifError(error);
      assert.equal(0, count);

      request('http://localhost:3000', function(error, response, body) {
        assert.ifError(error);
        assert.equal(1, response.headers['set-cookie'].length);
        var cookie = require('cookie').parse(response.headers['set-cookie'][0]);
        assert.ok(cookie['connect.sid']);
        underlyingDb.collection('mySessions').count({}, function(error, count) {
          assert.ifError(error);
          assert.equal(1, count);
          server.close();
          done();
        });
      });
    });
  

Keywords

FAQs

Package last updated on 12 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc