当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

koa-session-mongoose

Mongoose store for Koa sessions
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 汪德明
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

koa-session-mongoose

Mongoose storage layer for koa-session.

Installation

npm i --save koa-session-mongoose

OR

yarn add koa-session-mongoose

Usage

Prerequisites

This store requires node@>=8.3.0, koa@>=2.0.0, and koa-session@>=5.0.0.

It has a peer-dependency of mongoose@>=5.0.0.

If you are using older dependencies, consider using koa-session-mongoose@^1.0.0 or koa-session-mongoose@^2.0.0.

Code Examples

const Koa = require("koa");
const mongoose = require("mongoose");
const MongooseStore = require("koa-session-mongoose");
const session = require("koa-session");

// mongoose connection must exist before creating a store with koa-session-mongoose
mongoose.connect("mongodb://some_host/some_db");

const app = new Koa();

// needed for cookie-signing
app.keys = ["some secret key"];

app.use(session({ store: new MongooseStore() }, app));

app.use(async ctx => {
  const { session } = ctx;
  let n = session.views || 0;
  session.views = ++n;
  ctx.body = `${n} view(s)`;
});

app.listen(3000);

You can optionally specify collection name (collection), model name (name), expiration time in seconds (expires), and Mongoose connection (connection):

async function init (uri) => {
  const connection = await mongoose.connect(uri, {
    useCreateIndex: true,
    useFindAndModify: false,
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  app.use(session({
    store: new MongooseStore({
      collection: 'appSessions',
      connection: connection,
      expires: 86400, // 1 day is the default
      name: 'AppSession'
    })
  }, app));
}

Related Modules

Development

Merge requests should be submitted to https://gitlab.com/wondermonger/koa-session-mongoose.

A mirror of the project will persist at https://github.com/mjbondra/koa-session-mongoose, but all future development will be directed to the new repository.

Installation

npm i

Linting

npm run lint

Testing

# all tests
npm run test

# integration tests
npm run test:integration

# unit tests
npm run test:unit

If you require a MongoDB URI other than the default (mongodb://127.0.0.1/koa-session-mongoose), specify it as follows before executing npm run test or npm run test:integration:

MONGODB_URI="mongodb://[username:password@]host[:port]/[database]"

License

The MIT License (MIT)

Copyright (c) 2013-2019 Michael J. Bondra mjbondra@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.

  • 按照githhub上使用koa-session-mongoose,将session存入数据库,却发现无法给session写入值。 koa-session-mongoose的使用示例: async function init (uri) => { const connection = await mongoose.connect(uri, { useMongoClient: true });

  • 1.mongoose基本使用 1.安装mongodb npm install mongodb 2.引入mongodb数据表,连接mongodb,通过node来对mongodb进行异步的增删改查 const mongodb = requrie('mongodb'); mongodb.MongoClient.connect("mongodb://localhost/db1", function(err

  • 在koa项目中安装koa-passport yarn add koa-passport -D 在app.js入口文件引入和使用 // token验证 const passport = require('koa-passport'); // token验证 app.use(passport.initialize()); app.use(passport.session()); // 回调到指定

 相关资料
  • Koa

    Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren

  • koa

    koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 历史 1. Express Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: var express = require('express'); var app = express(); app.get('/', function (req, res) {

  • Koa

    Koa 是下一代的 Node.js 的 Web 框架。由 Express 团队设计。旨在提供一个更小型、更富有表现力、更可靠的 Web 应用和 API 的开发基础。 Koa可以通过生成器摆脱回调,极大地改进错误处理。Koa核心不绑定任何中间件,但提供了优雅的一组可以快速和愉悦地编写服务器应用的方法。 示例代码: var koa = require('koa');var app = koa();//

  • Koa - HelloWorld 以上便是全部了,我们重点来看示例,我们只注册一个中间件, Hello Worler Server: <?php $app = new Application(); // ... $app->υse(function(Context $ctx) { $ctx->status = 200; $ctx->body = "<h1>Hello Worl

  • koa-log4js A wrapper for log4js-node which support Koa logger middleware.Log message is forked from Express (Connect) logger file. Note This branch is use to Koa v2.x.To use Koa v0.x & v1.x, please ch

  • koa-rudy 环境 node -v >=6.9.0pm2 启动 npm install npm run dev 开发环境 npm run dev || test || prod 接口测试 npm run mocha 推荐开发工具 vscode 实现 支持 async/await MVC架构(middleware-view-controller) RESTful a