MongoDB-backed session storage for koa and it's koa-generic-session module. Meant to be a well-maintained and fully-featured replacement for modules like koa-generic-session-mongo
This module exports a MongoDBStore
class that can be used to
store sessions in MongoDB.
The MongoDBStore class has 2 required options:
uri
: a MongoDB connection stringcollection
: the MongoDB collection to store sessions in
Note: You can pass a callback to the MongoDBStore
constructor,
but this is entirely optional. The module will manage the internal connection state for you.
const Koa = require('koa');
const session = require('koa-generic-session');
const convert = require('koa-convert');
const MongoDBStore = require('koa-mongodb-session');
const app = new Koa();
const store = new MongoDBStore(
{
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
collection: 'mySessions'
});
// Catch errors
store.on('error', function(error) {
assert.ifError(error);
assert.ok(false);
});
app.keys = ['This is a secret']
app.use(convert(session({
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
},
store
})))
app.use(ctx => {
ctx.body = `Hello ${JSON.stringify(ctx.session)}`;
});
app.listen(3000);
You should pass a callback to the MongoDBStore
constructor to catch
errors. If you don't pass a callback to the MongoDBStore
constructor,
MongoDBStore
will throw
if it can't connect.
const Koa = require('koa');
const session = require('koa-generic-session');
const convert = require('koa-convert');
const MongoDBStore = require('koa-mongodb-session');
const app = new Koa();
const store = new MongoDBStore(
{
uri: 'mongodb://bad.host:27000/connect_mongodb_session_test?connectTimeoutMS=10',
collection: 'mySessions'
},
function(error) {
// Should have gotten an error
});
store.on('error', function(error) {
// Also get an error here
});
app.keys = ['This is a secret']
app.use(convert(session({
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
},
store
})))
app.use(ctx => {
ctx.body = `Hello ${JSON.stringify(ctx.session)}`;
});
app.listen(3000);