https://github.com/trekjs/sessions-provider-mongodb
Sessions Mongodb Provider
https://github.com/trekjs/sessions-provider-mongodb
Last synced: 4 months ago
JSON representation
Sessions Mongodb Provider
- Host: GitHub
- URL: https://github.com/trekjs/sessions-provider-mongodb
- Owner: trekjs
- License: mit
- Created: 2016-10-16T17:26:26.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-08T00:31:30.000Z (over 8 years ago)
- Last Synced: 2025-08-25T23:55:40.334Z (4 months ago)
- Language: JavaScript
- Size: 93.8 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sessions-provider-mongodb
> Sessions Mongodb Provider
## Install
```console
$ npm install sessions-provider-mongodb --save
```
## Usage
```js
'use strict'
const Engine = require('trek-engine')
const sessions = require('trek-sessions')
const MongodbProvider = require('sessions-provider-mongodb')
async function start (port = 3000) {
const app = new Engine()
app.config.set('cookie', {
keys: ['trek', 'engine']
})
app.use(await sessions({
cookie: {
signed: false,
maxAge: 60 * 1000 // 1 minutes
},
provider: new MongodbProvider()
}))
app.use(async ctx => {
if (ctx.session.count) {
ctx.session.count++
} else {
ctx.session.count = 1
}
if (ctx.req.path === '/clear') {
ctx.session = null
await ctx.sessions.store.clear()
}
ctx.res.body = ctx.session
})
await app.run(port)
}
start().catch(console.error)
```
## API
* `get(sid)`
* `set(sid, sess, expires)`
* `has(sid)`
* `delete(sid)`
* `clear()`
* `quit()`
## Badges
---
> [fundon.me](https://fundon.me) ·
> GitHub [@Fangdun Cai](https://github.com/fundon) ·
> Twitter [@_fundon](https://twitter.com/_fundon)