connect-mongo模块
Posted lzy_tinyjoy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了connect-mongo模块相关的知识,希望对你有一定的参考价值。
简述:
session数据存储空间一般是在内存中开辟的,那么在内存中的session显然是存在极大的数据丢失的隐患的,比如系统掉电,所有的会话数据就会丢失,如果是证券交易所那么这种后果的严重性可想而知。所以为了解决这个问题可以将session持久化保存,比如保存到数据库。那么这篇博客就是介绍session持久化保存到mongoDB的工具connect-mongo。
connect-mongo介绍:
github地址: https://github.com/kcbanner/connect-mongo 正如上面所言,connect-mongo用于将session持久化到mongodb数据库。
兼容性:
- Support Express up to
5.0
- Support all Connect versions
- Support Mongoose
>= 2.6
,3.x
and4.x
- Support native MongoDB driver
>= 1.2
,2.x
- Support Node.js
0.10
,0.12
,4.x
,5.x
,6.x
and all io.js versions - Support MongoDB up to
3.2
connect-mongo使用:
express框架中使用如下:
①在express4.x和5.0使用如下:
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
app.use(session(
secret: 'foo',
store: new MongoStore(options)
));
const MongoStore = require('connect-mongo')(express);
app.use(express.session(
secret: 'foo',
store: new MongoStore(options)
));
配置说明:(下一行为上一行的翻译)
db
Database name OR fully instantiated node-mongo-native object- db 数据库名称
collection
Collection (optional, default:sessions
)- collection 集合名称,默认为sessions
host
MongoDB server hostname (optional, default:127.0.0.1
)- 数据库地址,默认为本机
port
MongoDB server port (optional, default:27017
)- 数据库端口,默认为27017
username
Username (optional)password
Password (optional)auto_reconnect
This is passed directly to the MongoDBServer
constructor as the auto_reconnect option (optional, default: false).ssl
Use SSL to connect to MongoDB (optional, default: false).url
Connection url of the form:mongodb://user:pass@host:port/database/collection
. If provided, information in the URL takes priority over the other options.mongoose_connection
in the form:someMongooseDb.connections[0]
to use an existing mongoose connection. (optional)stringify
If true, connect-mongo will serialize sessions usingJSON.stringify
before setting them, and deserialize them withJSON.parse
when getting them. (optional, default: true). This is useful if you are using types that MongoDB doesn't support.
以上是关于connect-mongo模块的主要内容,如果未能解决你的问题,请参考以下文章