Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # connect-mongo
  2. MongoDB session store for [Connect](https://github.com/senchalabs/connect) and [Express](http://expressjs.com/)
  3. [![npm version](https://img.shields.io/npm/v/connect-mongo.svg)](https://www.npmjs.com/package/connect-mongo)
  4. [![downloads](https://img.shields.io/npm/dm/connect-mongo.svg)](https://www.npmjs.com/package/connect-mongo)
  5. [![Build Status](https://travis-ci.org/jdesboeufs/connect-mongo.svg?branch=master)](https://travis-ci.org/jdesboeufs/connect-mongo)
  6. [![Coverage Status](https://coveralls.io/repos/jdesboeufs/connect-mongo/badge.svg?branch=master&service=github)](https://coveralls.io/github/jdesboeufs/connect-mongo?branch=master)
  7. [![Dependency Status](https://david-dm.org/jdesboeufs/connect-mongo.svg?style=flat)](https://david-dm.org/jdesboeufs/connect-mongo)
  8. [![Greenkeeper badge](https://badges.greenkeeper.io/jdesboeufs/connect-mongo.svg)](https://greenkeeper.io/)
  9. [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
  10. ## Compatibility
  11. * Support Express up to `5.0`
  12. * Support all Connect versions
  13. * Support [Mongoose](http://mongoosejs.com/index.html) `>= 4.1.2+`
  14. * Support [native MongoDB driver](http://mongodb.github.io/node-mongodb-native/) `>= 2.0.36`
  15. * Support Node.js 4, 6, 8 and 10
  16. * Support [MongoDB](https://www.mongodb.com/) `>= 3.0`
  17. For extended compatibility, see previous versions.
  18. ## Usage
  19. ### Express or Connect integration
  20. Express `4.x`, `5.0` and Connect `3.x`:
  21. ```js
  22. const session = require('express-session');
  23. const MongoStore = require('connect-mongo')(session);
  24. app.use(session({
  25. secret: 'foo',
  26. store: new MongoStore(options)
  27. }));
  28. ```
  29. Express `2.x`, `3.x` and Connect `1.x`, `2.x`:
  30. ```js
  31. const MongoStore = require('connect-mongo')(express);
  32. app.use(express.session({
  33. secret: 'foo',
  34. store: new MongoStore(options)
  35. }));
  36. ```
  37. For Connect `1.x` and `2.x`, just replace `express` by `connect`.
  38. ### Connection to MongoDB
  39. In many circumstances, `connect-mongo` will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.
  40. Alternatively, you can configure `connect-mongo` to establish a new connection.
  41. #### Re-use a Mongoose connection
  42. ```js
  43. const mongoose = require('mongoose');
  44. // Basic usage
  45. mongoose.connect(connectionOptions);
  46. app.use(session({
  47. store: new MongoStore({ mongooseConnection: mongoose.connection })
  48. }));
  49. // Advanced usage
  50. const connection = mongoose.createConnection(connectionOptions);
  51. app.use(session({
  52. store: new MongoStore({ mongooseConnection: connection })
  53. }));
  54. ```
  55. #### Re-use a native MongoDB driver connection (or a promise)
  56. In this case, you just have to give your `Db` instance to `connect-mongo`.
  57. If the connection is not opened, `connect-mongo` will do it for you.
  58. ```js
  59. /*
  60. ** There are many ways to create dbInstance.
  61. ** You should refer to the driver documentation.
  62. */
  63. app.use(session({
  64. store: new MongoStore({ db: dbInstance })
  65. }));
  66. ```
  67. Or just give a promise...
  68. ```js
  69. app.use(session({
  70. store: new MongoStore({ dbPromise: dbInstancePromise })
  71. }));
  72. ```
  73. #### Create a new connection from a MongoDB connection string
  74. [MongoDB connection strings](http://docs.mongodb.org/manual/reference/connection-string/) are __the best way__ to configure a new connection. For advanced usage, [more options](http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connect-options) can be configured with `mongoOptions` property.
  75. ```js
  76. // Basic usage
  77. app.use(session({
  78. store: new MongoStore({ url: 'mongodb://localhost/test-app' })
  79. }));
  80. // Advanced usage
  81. app.use(session({
  82. store: new MongoStore({
  83. url: 'mongodb://user12345:foobar@localhost/test-app?authSource=admins&w=1',
  84. mongoOptions: advancedOptions // See below for details
  85. })
  86. }));
  87. ```
  88. ## Events
  89. A `MongoStore` instance will emit the following events:
  90. | Event name | Description | Payload
  91. | ----- | ----- | ----- |
  92. | `create` | A session has been created | `sessionId` |
  93. | `touch` | A session has been touched (but not modified) | `sessionId` |
  94. | `update` | A session has been updated | `sessionId` |
  95. | `set` | A session has been created OR updated _(for compatibility purpose)_ | `sessionId` |
  96. | `destroy` | A session has been destroyed | `sessionId` |
  97. ## Session expiration
  98. When the session cookie has an expiration date, `connect-mongo` will use it.
  99. Otherwise, it will create a new one, using `ttl` option.
  100. ```js
  101. app.use(session({
  102. store: new MongoStore({
  103. url: 'mongodb://localhost/test-app',
  104. ttl: 14 * 24 * 60 * 60 // = 14 days. Default
  105. })
  106. }));
  107. ```
  108. __Note:__ Each time an user interacts with the server, its session expiration date is refreshed.
  109. ## Remove expired sessions
  110. By default, `connect-mongo` uses MongoDB's TTL collection feature (2.2+) to have mongod automatically remove expired sessions. But you can change this behavior.
  111. ### Set MongoDB to clean expired sessions (default mode)
  112. `connect-mongo` will create a TTL index for you at startup. You MUST have MongoDB 2.2+ and administration permissions.
  113. ```js
  114. app.use(session({
  115. store: new MongoStore({
  116. url: 'mongodb://localhost/test-app',
  117. autoRemove: 'native' // Default
  118. })
  119. }));
  120. ```
  121. __Note:__ If you use `connect-mongo` in a very concurrent environment, you should avoid this mode and prefer setting the index yourself, once!
  122. ### Set the compatibility mode
  123. You have an older MongoDB version (compatible with connect-mongo) or you can't or don't want to create a TTL index.
  124. `connect-mongo` will take care of removing expired sessions, using defined interval.
  125. ```js
  126. app.use(session({
  127. store: new MongoStore({
  128. url: 'mongodb://localhost/test-app',
  129. autoRemove: 'interval',
  130. autoRemoveInterval: 10 // In minutes. Default
  131. })
  132. }));
  133. ```
  134. ### Disable expired sessions cleaning
  135. You are in production environnement and/or you manage the TTL index elsewhere.
  136. ```js
  137. app.use(session({
  138. store: new MongoStore({
  139. url: 'mongodb://localhost/test-app',
  140. autoRemove: 'disabled'
  141. })
  142. }));
  143. ```
  144. ## Lazy session update
  145. If you are using [express-session](https://github.com/expressjs/session) >= [1.10.0](https://github.com/expressjs/session/releases/tag/v1.10.0) and don't want to resave all the session on database every single time that the user refresh the page, you can lazy update the session, by limiting a period of time.
  146. ```js
  147. app.use(express.session({
  148. secret: 'keyboard cat',
  149. saveUninitialized: false, // don't create session until something stored
  150. resave: false, //don't save session if unmodified
  151. store: new MongoStore({
  152. url: 'mongodb://localhost/test-app',
  153. touchAfter: 24 * 3600 // time period in seconds
  154. })
  155. }));
  156. ```
  157. by doing this, setting `touchAfter: 24 * 3600` you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)
  158. ## More options
  159. - `collection` Collection (default: `sessions`)
  160. - `fallbackMemory` Fallback to `MemoryStore`. Useful if you want to use MemoryStore in some case, like in development environment.
  161. - `stringify` If true, connect-mongo will serialize sessions using `JSON.stringify` before
  162. setting them, and deserialize them with `JSON.parse` when getting them.
  163. (optional, default: true). This is useful if you are using types that
  164. MongoDB doesn't support.
  165. - `serialize` Custom hook for serializing sessions to MongoDB. This is helpful if you need
  166. to modify the session before writing it out.
  167. - `unserialize` Custom hook for unserializing sessions from MongoDB. This can be used in
  168. scenarios where you need to support different types of serializations
  169. (e.g., objects and JSON strings) or need to modify the session before using
  170. it in your app.
  171. - `transformId` (optional) Transform original sessionId in whatever you want to use as storage key.
  172. ## Tests
  173. npm test
  174. The tests use a database called `connect-mongo-test`.
  175. ## Showcase
  176. Open source projects and production apps using `connect-mongo`. Feel free to add yours in a pull request.
  177. * [Builder Book](https://github.com/builderbook/builderbook): Open source web app to write and host documentation or sell books. Built with React, Material-UI, Next, Express, Mongoose, MongoDB.
  178. ## License
  179. The MIT License