mongo-express-session-store
v1.0.16
Published
Middleware to store expression session data in mongoDB Database. Using the official mogoDB driver, less dependencies and possibility to reuse the client / database for other database tasks.
Downloads
12
Readme
MongoDB session store for using with express and express-session
This middleware uses the the official MongoDB driver for Node.js.
I have tried to keep the dependencies as small as possible.
It is also possible to get the MongoDB client, database and collection
for other database tasks in the app.
MongoDB recommends only to use one client per app.
The getMongo
function will do the job. In a callback it delivers after connection the client, database and collection.
Installation
After you've created your own project using npm init
, you can run:
npm install mongo-express-session-store
This will download the session store and add a dependency entry in your package.json
file.
Usage
Quickstart
- Install the package
npm install mongo-express-session-store
- Import the package in your source file
cjs:
const mongoDBSessionStore = require('mongo-express-session-store')
ES6+:
import mongoDBSessionStore from 'mongo-express-session-store'
- Get the store class
const MongoDBSessionStore = mongoDBSessionStore(session)
- Create the store object
const MONGODB_URL = 'mongodb://USER:[email protected]:27017/?authSource=admin'
const mongoStore = new MongoDBSessionStore({
mongoUrl: MONGODB_URL,
databaseName: "sessionTest",
collectionName: "sessions"
})
- Set the store in the session
app.use(session({
secret: 'Top secret ...',
cookie: {
maxAge: 3600*24*1000,
},
resave: false,
saveUninitialized: true,
store: mongoStore
})
)
- Get the MongoDB client, database and collection objects (optional)
let mongoClient = null
let mongoDatabase = null
mongoStore.getMongo((client, database) => {
mongoClient = client
mongoDatabase = database
console.log('Client connected')
})
Complete working example
cjs
'use strict'
const express = require('express')
const session = require('express-session')
+const mongoDBSessionStore = require('mongo-express-session-store')
const app = express()
+const MongoDBSessionStore = mongoDBSessionStore(session)
+const MONGODB_URL = 'mongodb://USER:[email protected]:27017/?authSource=admin'
+const mongoStore = new MongoDBSessionStore(
+ {
+ mongoUrl: MONGODB_URL,
+ databaseName: "sessionTest",
+ collectionName: "sessions"
+ }
+)
+let mongoClient = null
+mongoStore.getMongo((client, database) => {mongoClient = client; console.log('Client connected')})
app.use(session(
{
secret: 'Top secret ...',
cookie: {
maxAge: 3600*24*1000,
},
resave: false,
saveUninitialized: true,
+ store: mongoStore
}
))
app.get('/', (req, res) => {
req.session.testValue = "XXX"
res.send("Hello")
})
app.get('/test', (req, res) => {
res.send(req.session.testValue)
})
app.listen(3000)```
ES6
'use strict'
import express from 'express'
import session from 'express-session'
+import mongoDBSessionStore from 'mongo-express-session-store'
const app = express()
+const MongoDBSessionStore = mongoDBSessionStore(session)
+const MONGODB_URL = 'mongodb://USER:[email protected]:27017/?authSource=admin'
+const mongoStore = new MongoDBSessionStore(
+ {
+ mongoUrl: MONGODB_URL,
+ databaseName: "sessionTest",
+ collectionName: "sessions"
+ }
+)
+let mongoClient = null
+mongoStore.getMongo((client, database) => {mongoClient = client; console.log('Client connected')})
app.use(session(
{
secret: 'Ich weiss von nix',
cookie: {
maxAge: 3600*24*1000,
},
resave: false,
saveUninitialized: true,
+ store: mongoStore
}
))
app.get('/', (req, res) => {
req.session.testValue = "XXX"
res.send("Hello")
})
app.get('/test', (req, res) => {
res.send(req.session.testValue)
})
app.listen(3000)
Example
In the doc
directory there is the source code for the example.
Test
I have done some tests with jest
For this you need to create an .env file in the root of this project it must contain the MongoDB URL: example:
MONGODB_URL='mongodb://USER:[email protected]:27017/?authSource=admin'
You have to urlencode
USER
andPASS
For the test I use the following database and collection names:
databaseName: "sessionUnitTest"
collectionName: "sessions"
Then run the test with npm test
.
License
© 2023 Reiner Pröls