Skip to content

Commit

Permalink
Adding shutdown function and updating Readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanb committed Nov 24, 2023
1 parent 752b168 commit 0179a2a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 19 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "demo",
"version": "0.2.3",
"version": "0.3.0-alpha.1",
"private": true,
"type": "module",
"scripts": {
Expand Down
8 changes: 2 additions & 6 deletions demo/src/app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import createError from 'http-errors'
import express from 'express'
import logger from 'morgan'
import session from 'express-session'
import { MongoSessionStore } from '@iwsio/mongodb-express-session'
import { store } from './store.mjs'
import { nanoid } from 'nanoid'

export const app = express()

const mongoStore = new MongoSessionStore({
uri: 'mongodb://localhost:27017/express_sessions'
})

app.use(logger('dev'))

app.use(session({
secret: 'keyboard cat',
resave: false,
store: mongoStore,
store,
saveUninitialized: false,
rolling: true,
cookie: { httpOnly: true, maxAge: 1000 * 60 * 60 * 24 * 7, sameSite: 'strict' },
Expand Down
10 changes: 9 additions & 1 deletion demo/src/bin/web.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import debugInit from 'debug'
import { createServer } from 'http'
import { app } from '../app.mjs'
import { store } from '../store.mjs'

const debug = debugInit('demo:server')
/**
Expand Down Expand Up @@ -76,11 +77,18 @@ function onError(error) {
/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
const addr = server.address()
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr!.port
debug('Listening on ' + bind)
}

function shutdownHandler(_sig: any, _n: any) {
if (server != null) server.close()
store.shutdown()
}

process.on('SIGTERM', shutdownHandler)
process.on('SIGINT', shutdownHandler)
11 changes: 11 additions & 0 deletions demo/src/store.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MongoSessionStore } from '@iwsio/mongodb-express-session'
import debugM from 'debug'

const debug = debugM('demo:session-store')

export const store = new MongoSessionStore({
uri: 'mongodb://localhost:27017/express_sessions'
})

store.on('info', debug)
store.on('error', console.error)
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iwsio/mongodb-express-session-root",
"version": "0.2.3",
"version": "0.3.0-alpha.2",
"description": "",
"scripts": {
"prebuild": "tsc --build --clean && npm run prebuild -ws --if-present",
Expand Down
31 changes: 27 additions & 4 deletions package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ type MongoSessionStoreOptions = {
Setting up an express server with `express-sessions` and using this MongoSessionStore as its store.

```ts
// app.mts
// store.mts
import { MongoSessionStore } from '@iwsio/mongodb-express-session'

// create a store with default options and custom mongo uri
export const store = new MongoSessionStore({ uri: 'mongodb://localhost/express_sessions' })

store.on('info', console.log)
store.on('error', console.error)


// app.mts
import express from 'express'
import session from 'express-session'
import { store as mongoStore } from './store.mjs'

// create a store with default options and custom mongo uri
const store = new MongoSessionStore({ uri: 'mongodb://localhost/express_sessions' })

// optional, listen to store errors
store.on('error', function(error: any) {
console.error(error)
Expand All @@ -48,6 +54,21 @@ app.use(session({
cookie: { httpOnly: true, sameSite: 'strict' },
name: 'connect.sid'
}))


// ./bin/web.mts
// Don't forget to close the db connection on shutdown
import { store as mongoStore } from './store.mjs'

//... server setup code above

function shutdownHandler(_sig: any, _n: any) {
if (server != null) server.close()
mongoStore.shutdown()
}

process.on('SIGTERM', shutdownHandler)
process.on('SIGINT', shutdownHandler)
```

## Another example
Expand Down Expand Up @@ -94,6 +115,8 @@ npm run build
npm start -w demo
```

`Ctrl-C` to stop the server.


### Test the cookie creation
Curl the site. Without providing an existing cookie, it should create one for you. You'll see it in the response as a `Set-Cookie:` header.
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iwsio/mongodb-express-session",
"version": "0.2.3",
"version": "0.3.0-alpha.2",
"description": "ESM MongoDB session store for Express. Supports TTL and touch.",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
12 changes: 11 additions & 1 deletion package/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class MongoSessionStore extends Store {
this.client.connect()
.then(() => {
this.connected = true
this.emit('info', 'Connected to MongoDB')
this.emit('info', 'Connected to MongoDB for session store.')
this.checkIndexes()
})
.catch((err: any) => {
Expand All @@ -90,6 +90,16 @@ export class MongoSessionStore extends Store {
}
}

/**
* Closes the MongoDB connection.
*/
public shutdown() {
if (this.connected) {
this.emit('info', 'Closing MongoDB connection to session store.')
this.client.close()
}
}

private _getTTL(sess: SessionData) {
if (typeof this.ttl === 'function') {
return this.ttl(sess)
Expand Down

0 comments on commit 0179a2a

Please sign in to comment.