npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

29

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

  1. Install the package
npm install mongo-express-session-store
  1. Import the package in your source file
cjs:
const mongoDBSessionStore = require('mongo-express-session-store')

ES6+:
import mongoDBSessionStore from 'mongo-express-session-store'
  1. Get the store class
const MongoDBSessionStore = mongoDBSessionStore(session)
  1. 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"
})
  1. Set the store in the session
app.use(session({
    secret: 'Top secret ...',
    cookie: {
        maxAge: 3600*24*1000,
    },
    resave: false,
    saveUninitialized: true,
    store: mongoStore
    })
)
  1. 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 and PASS

For the test I use the following database and collection names:

databaseName: "sessionUnitTest"

collectionName: "sessions"

Then run the test with npm test.

License

ISC

© 2023 Reiner Pröls