Skip to content

trekjs/sessions-provider-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sessions-provider-mongodb

Sessions Mongodb Provider

Install

$ npm install sessions-provider-mongodb --save

Usage

'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

NPM version MIT License Codecov Linux Build Window Build


fundon.me  ·  GitHub @Fangdun Cai  ·  Twitter @_fundon