Error: Connection strategy not found MongoDB

Spencer Hire picture Spencer Hire · Jan 16, 2016 · Viewed 15.3k times · Source

here is a simple connection to use express session store, it keeps banging out this error even though the text is right from the book. I am pretty sure is has something to do with 'new MongoStore' object initialization.

var express = require('express'),
    expressSession = require('express-session');

var MongoStore = require('connect-mongo/es5')(expressSession);
var sessionStore = new MongoStore({
  host: '127.0.0.1',
  port: '27017',
  db: 'session'
});

var app = express()
    .use(expressSession({
      secret: 'my secret sign key',
      store: sessionStore
     }))
    .use('/home', function (req, res) {
      if (req.session.views) {
        req.session.views++;
      }
      else {
        req.session.views = 1;
      }
      res.end('Total views for you:' + req.session.views);
    })
    .use('/reset', function(req, res) {
      delete req.session.views;
      res.end('Cleared all your views');
    })
    .listen(3000);

Answer

alturium picture alturium · Sep 6, 2016

Add URL to new MongoStore()

  var sessionStore = new MongoStore({
        host: '127.0.0.1',
        port: '27017',
        db: 'session',
        url: 'mongodb://localhost:27017/demo'
    });

The code in the question is from the book Beginning Node.js by Basarat Ali Syed.