node.jsexpresssessionconnect-mongo

In Express.js, how do I generate server-side sessions for logged in users only?mo


Currently, I'm using express-session:

https://github.com/expressjs/session

combined with

connect-mongo:

https://github.com/jdesboeufs/connect-mongo

I use the boilerplate examples from the site, and I can successfully generate sessions for logged in users.

But I also have a health monitor which pings the site every few seconds, and this is also generating sessions on the server which is clogging up the database.

How can I generate sessions for logged in users only?


Solution

  • you can define a "control" endpoint in your express app. something like

    const app = express()
    app.get('/ping', (req,res) => res.send('pong'))
    app.use(sessions...)
    

    as you know express mechanism works from top to bottom so the get request have the priority upon the sessions middleware :)