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 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. # express-session
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][travis-image]][travis-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. [![Gratipay][gratipay-image]][gratipay-url]
  7. ## Installation
  8. This is a [Node.js](https://nodejs.org/en/) module available through the
  9. [npm registry](https://www.npmjs.com/). Installation is done using the
  10. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  11. ```sh
  12. $ npm install express-session
  13. ```
  14. ## API
  15. ```js
  16. var session = require('express-session')
  17. ```
  18. ### session(options)
  19. Create a session middleware with the given `options`.
  20. **Note** Session data is _not_ saved in the cookie itself, just the session ID.
  21. Session data is stored server-side.
  22. **Note** Since version 1.5.0, the [`cookie-parser` middleware](https://www.npmjs.com/package/cookie-parser)
  23. no longer needs to be used for this module to work. This module now directly reads
  24. and writes cookies on `req`/`res`. Using `cookie-parser` may result in issues
  25. if the `secret` is not the same between this module and `cookie-parser`.
  26. **Warning** The default server-side session storage, `MemoryStore`, is _purposely_
  27. not designed for a production environment. It will leak memory under most
  28. conditions, does not scale past a single process, and is meant for debugging and
  29. developing.
  30. For a list of stores, see [compatible session stores](#compatible-session-stores).
  31. #### Options
  32. `express-session` accepts these properties in the options object.
  33. ##### cookie
  34. Settings object for the session ID cookie. The default value is
  35. `{ path: '/', httpOnly: true, secure: false, maxAge: null }`.
  36. The following are options that can be set in this object.
  37. ##### cookie.domain
  38. Specifies the value for the `Domain` `Set-Cookie` attribute. By default, no domain
  39. is set, and most clients will consider the cookie to apply to only the current
  40. domain.
  41. ##### cookie.expires
  42. Specifies the `Date` object to be the value for the `Expires` `Set-Cookie` attribute.
  43. By default, no expiration is set, and most clients will consider this a
  44. "non-persistent cookie" and will delete it on a condition like exiting a web browser
  45. application.
  46. **Note** If both `expires` and `maxAge` are set in the options, then the last one
  47. defined in the object is what is used.
  48. **Note** The `expires` option should not be set directly; instead only use the `maxAge`
  49. option.
  50. ##### cookie.httpOnly
  51. Specifies the `boolean` value for the `HttpOnly` `Set-Cookie` attribute. When truthy,
  52. the `HttpOnly` attribute is set, otherwise it is not. By default, the `HttpOnly`
  53. attribute is set.
  54. **Note** be careful when setting this to `true`, as compliant clients will not allow
  55. client-side JavaScript to see the cookie in `document.cookie`.
  56. ##### cookie.maxAge
  57. Specifies the `number` (in milliseconds) to use when calculating the `Expires`
  58. `Set-Cookie` attribute. This is done by taking the current server time and adding
  59. `maxAge` milliseconds to the value to calculate an `Expires` datetime. By default,
  60. no maximum age is set.
  61. **Note** If both `expires` and `maxAge` are set in the options, then the last one
  62. defined in the object is what is used.
  63. ##### cookie.path
  64. Specifies the value for the `Path` `Set-Cookie`. By default, this is set to `'/'`, which
  65. is the root path of the domain.
  66. ##### cookie.sameSite
  67. Specifies the `boolean` or `string` to be the value for the `SameSite` `Set-Cookie` attribute.
  68. - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
  69. - `false` will not set the `SameSite` attribute.
  70. - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
  71. - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
  72. More information about the different enforcement levels can be found in the specification
  73. https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-4.1.1
  74. **Note** This is an attribute that has not yet been fully standardized, and may change in
  75. the future. This also means many clients may ignore this attribute until they understand it.
  76. ##### cookie.secure
  77. Specifies the `boolean` value for the `Secure` `Set-Cookie` attribute. When truthy,
  78. the `Secure` attribute is set, otherwise it is not. By default, the `Secure`
  79. attribute is not set.
  80. **Note** be careful when setting this to `true`, as compliant clients will not send
  81. the cookie back to the server in the future if the browser does not have an HTTPS
  82. connection.
  83. Please note that `secure: true` is a **recommended** option. However, it requires
  84. an https-enabled website, i.e., HTTPS is necessary for secure cookies. If `secure`
  85. is set, and you access your site over HTTP, the cookie will not be set. If you
  86. have your node.js behind a proxy and are using `secure: true`, you need to set
  87. "trust proxy" in express:
  88. ```js
  89. var app = express()
  90. app.set('trust proxy', 1) // trust first proxy
  91. app.use(session({
  92. secret: 'keyboard cat',
  93. resave: false,
  94. saveUninitialized: true,
  95. cookie: { secure: true }
  96. }))
  97. ```
  98. For using secure cookies in production, but allowing for testing in development,
  99. the following is an example of enabling this setup based on `NODE_ENV` in express:
  100. ```js
  101. var app = express()
  102. var sess = {
  103. secret: 'keyboard cat',
  104. cookie: {}
  105. }
  106. if (app.get('env') === 'production') {
  107. app.set('trust proxy', 1) // trust first proxy
  108. sess.cookie.secure = true // serve secure cookies
  109. }
  110. app.use(session(sess))
  111. ```
  112. The `cookie.secure` option can also be set to the special value `'auto'` to have
  113. this setting automatically match the determined security of the connection. Be
  114. careful when using this setting if the site is available both as HTTP and HTTPS,
  115. as once the cookie is set on HTTPS, it will no longer be visible over HTTP. This
  116. is useful when the Express `"trust proxy"` setting is properly setup to simplify
  117. development vs production configuration.
  118. ##### genid
  119. Function to call to generate a new session ID. Provide a function that returns
  120. a string that will be used as a session ID. The function is given `req` as the
  121. first argument if you want to use some value attached to `req` when generating
  122. the ID.
  123. The default value is a function which uses the `uid-safe` library to generate IDs.
  124. **NOTE** be careful to generate unique IDs so your sessions do not conflict.
  125. ```js
  126. app.use(session({
  127. genid: function(req) {
  128. return genuuid() // use UUIDs for session IDs
  129. },
  130. secret: 'keyboard cat'
  131. }))
  132. ```
  133. ##### name
  134. The name of the session ID cookie to set in the response (and read from in the
  135. request).
  136. The default value is `'connect.sid'`.
  137. **Note** if you have multiple apps running on the same hostname (this is just
  138. the name, i.e. `localhost` or `127.0.0.1`; different schemes and ports do not
  139. name a different hostname), then you need to separate the session cookies from
  140. each other. The simplest method is to simply set different `name`s per app.
  141. ##### proxy
  142. Trust the reverse proxy when setting secure cookies (via the "X-Forwarded-Proto"
  143. header).
  144. The default value is `undefined`.
  145. - `true` The "X-Forwarded-Proto" header will be used.
  146. - `false` All headers are ignored and the connection is considered secure only
  147. if there is a direct TLS/SSL connection.
  148. - `undefined` Uses the "trust proxy" setting from express
  149. ##### resave
  150. Forces the session to be saved back to the session store, even if the session
  151. was never modified during the request. Depending on your store this may be
  152. necessary, but it can also create race conditions where a client makes two
  153. parallel requests to your server and changes made to the session in one
  154. request may get overwritten when the other request ends, even if it made no
  155. changes (this behavior also depends on what store you're using).
  156. The default value is `true`, but using the default has been deprecated,
  157. as the default will change in the future. Please research into this setting
  158. and choose what is appropriate to your use-case. Typically, you'll want
  159. `false`.
  160. How do I know if this is necessary for my store? The best way to know is to
  161. check with your store if it implements the `touch` method. If it does, then
  162. you can safely set `resave: false`. If it does not implement the `touch`
  163. method and your store sets an expiration date on stored sessions, then you
  164. likely need `resave: true`.
  165. ##### rolling
  166. Force a session identifier cookie to be set on every response. The expiration
  167. is reset to the original [`maxAge`](#cookiemaxage), resetting the expiration
  168. countdown.
  169. The default value is `false`.
  170. **Note** When this option is set to `true` but the `saveUninitialized` option is
  171. set to `false`, the cookie will not be set on a response with an uninitialized
  172. session.
  173. ##### saveUninitialized
  174. Forces a session that is "uninitialized" to be saved to the store. A session is
  175. uninitialized when it is new but not modified. Choosing `false` is useful for
  176. implementing login sessions, reducing server storage usage, or complying with
  177. laws that require permission before setting a cookie. Choosing `false` will also
  178. help with race conditions where a client makes multiple parallel requests
  179. without a session.
  180. The default value is `true`, but using the default has been deprecated, as the
  181. default will change in the future. Please research into this setting and
  182. choose what is appropriate to your use-case.
  183. **Note** if you are using Session in conjunction with PassportJS, Passport
  184. will add an empty Passport object to the session for use after a user is
  185. authenticated, which will be treated as a modification to the session, causing
  186. it to be saved. *This has been fixed in PassportJS 0.3.0*
  187. ##### secret
  188. **Required option**
  189. This is the secret used to sign the session ID cookie. This can be either a string
  190. for a single secret, or an array of multiple secrets. If an array of secrets is
  191. provided, only the first element will be used to sign the session ID cookie, while
  192. all the elements will be considered when verifying the signature in requests.
  193. ##### store
  194. The session store instance, defaults to a new `MemoryStore` instance.
  195. ##### unset
  196. Control the result of unsetting `req.session` (through `delete`, setting to `null`,
  197. etc.).
  198. The default value is `'keep'`.
  199. - `'destroy'` The session will be destroyed (deleted) when the response ends.
  200. - `'keep'` The session in the store will be kept, but modifications made during
  201. the request are ignored and not saved.
  202. ### req.session
  203. To store or access session data, simply use the request property `req.session`,
  204. which is (generally) serialized as JSON by the store, so nested objects
  205. are typically fine. For example below is a user-specific view counter:
  206. ```js
  207. // Use the session middleware
  208. app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
  209. // Access the session as req.session
  210. app.get('/', function(req, res, next) {
  211. if (req.session.views) {
  212. req.session.views++
  213. res.setHeader('Content-Type', 'text/html')
  214. res.write('<p>views: ' + req.session.views + '</p>')
  215. res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
  216. res.end()
  217. } else {
  218. req.session.views = 1
  219. res.end('welcome to the session demo. refresh!')
  220. }
  221. })
  222. ```
  223. #### Session.regenerate(callback)
  224. To regenerate the session simply invoke the method. Once complete,
  225. a new SID and `Session` instance will be initialized at `req.session`
  226. and the `callback` will be invoked.
  227. ```js
  228. req.session.regenerate(function(err) {
  229. // will have a new session here
  230. })
  231. ```
  232. #### Session.destroy(callback)
  233. Destroys the session and will unset the `req.session` property.
  234. Once complete, the `callback` will be invoked.
  235. ```js
  236. req.session.destroy(function(err) {
  237. // cannot access session here
  238. })
  239. ```
  240. #### Session.reload(callback)
  241. Reloads the session data from the store and re-populates the
  242. `req.session` object. Once complete, the `callback` will be invoked.
  243. ```js
  244. req.session.reload(function(err) {
  245. // session updated
  246. })
  247. ```
  248. #### Session.save(callback)
  249. Save the session back to the store, replacing the contents on the store with the
  250. contents in memory (though a store may do something else--consult the store's
  251. documentation for exact behavior).
  252. This method is automatically called at the end of the HTTP response if the
  253. session data has been altered (though this behavior can be altered with various
  254. options in the middleware constructor). Because of this, typically this method
  255. does not need to be called.
  256. There are some cases where it is useful to call this method, for example,
  257. redirects, long-lived requests or in WebSockets.
  258. ```js
  259. req.session.save(function(err) {
  260. // session saved
  261. })
  262. ```
  263. #### Session.touch()
  264. Updates the `.maxAge` property. Typically this is
  265. not necessary to call, as the session middleware does this for you.
  266. ### req.session.id
  267. Each session has a unique ID associated with it. This property is an
  268. alias of [`req.sessionID`](#reqsessionid-1) and cannot be modified.
  269. It has been added to make the session ID accessible from the `session`
  270. object.
  271. ### req.session.cookie
  272. Each session has a unique cookie object accompany it. This allows
  273. you to alter the session cookie per visitor. For example we can
  274. set `req.session.cookie.expires` to `false` to enable the cookie
  275. to remain for only the duration of the user-agent.
  276. #### Cookie.maxAge
  277. Alternatively `req.session.cookie.maxAge` will return the time
  278. remaining in milliseconds, which we may also re-assign a new value
  279. to adjust the `.expires` property appropriately. The following
  280. are essentially equivalent
  281. ```js
  282. var hour = 3600000
  283. req.session.cookie.expires = new Date(Date.now() + hour)
  284. req.session.cookie.maxAge = hour
  285. ```
  286. For example when `maxAge` is set to `60000` (one minute), and 30 seconds
  287. has elapsed it will return `30000` until the current request has completed,
  288. at which time `req.session.touch()` is called to reset `req.session.maxAge`
  289. to its original value.
  290. ```js
  291. req.session.cookie.maxAge // => 30000
  292. ```
  293. ### req.sessionID
  294. To get the ID of the loaded session, access the request property
  295. `req.sessionID`. This is simply a read-only value set when a session
  296. is loaded/created.
  297. ## Session Store Implementation
  298. Every session store _must_ be an `EventEmitter` and implement specific
  299. methods. The following methods are the list of **required**, **recommended**,
  300. and **optional**.
  301. * Required methods are ones that this module will always call on the store.
  302. * Recommended methods are ones that this module will call on the store if
  303. available.
  304. * Optional methods are ones this module does not call at all, but helps
  305. present uniform stores to users.
  306. For an example implementation view the [connect-redis](http://github.com/visionmedia/connect-redis) repo.
  307. ### store.all(callback)
  308. **Optional**
  309. This optional method is used to get all sessions in the store as an array. The
  310. `callback` should be called as `callback(error, sessions)`.
  311. ### store.destroy(sid, callback)
  312. **Required**
  313. This required method is used to destroy/delete a session from the store given
  314. a session ID (`sid`). The `callback` should be called as `callback(error)` once
  315. the session is destroyed.
  316. ### store.clear(callback)
  317. **Optional**
  318. This optional method is used to delete all sessions from the store. The
  319. `callback` should be called as `callback(error)` once the store is cleared.
  320. ### store.length(callback)
  321. **Optional**
  322. This optional method is used to get the count of all sessions in the store.
  323. The `callback` should be called as `callback(error, len)`.
  324. ### store.get(sid, callback)
  325. **Required**
  326. This required method is used to get a session from the store given a session
  327. ID (`sid`). The `callback` should be called as `callback(error, session)`.
  328. The `session` argument should be a session if found, otherwise `null` or
  329. `undefined` if the session was not found (and there was no error). A special
  330. case is made when `error.code === 'ENOENT'` to act like `callback(null, null)`.
  331. ### store.set(sid, session, callback)
  332. **Required**
  333. This required method is used to upsert a session into the store given a
  334. session ID (`sid`) and session (`session`) object. The callback should be
  335. called as `callback(error)` once the session has been set in the store.
  336. ### store.touch(sid, session, callback)
  337. **Recommended**
  338. This recommended method is used to "touch" a given session given a
  339. session ID (`sid`) and session (`session`) object. The `callback` should be
  340. called as `callback(error)` once the session has been touched.
  341. This is primarily used when the store will automatically delete idle sessions
  342. and this method is used to signal to the store the given session is active,
  343. potentially resetting the idle timer.
  344. ## Compatible Session Stores
  345. The following modules implement a session store that is compatible with this
  346. module. Please make a PR to add additional modules :)
  347. [![★][aerospike-session-store-image] aerospike-session-store][aerospike-session-store-url] A session store using [Aerospike](http://www.aerospike.com/).
  348. [aerospike-session-store-url]: https://www.npmjs.com/package/aerospike-session-store
  349. [aerospike-session-store-image]: https://img.shields.io/github/stars/aerospike/aerospike-session-store-expressjs.svg?label=%E2%98%85
  350. [![★][cassandra-store-image] cassandra-store][cassandra-store-url] An Apache Cassandra-based session store.
  351. [cassandra-store-url]: https://www.npmjs.com/package/cassandra-store
  352. [cassandra-store-image]: https://img.shields.io/github/stars/webcc/cassandra-store.svg?label=%E2%98%85
  353. [![★][cluster-store-image] cluster-store][cluster-store-url] A wrapper for using in-process / embedded
  354. stores - such as SQLite (via knex), leveldb, files, or memory - with node cluster (desirable for Raspberry Pi 2
  355. and other multi-core embedded devices).
  356. [cluster-store-url]: https://www.npmjs.com/package/cluster-store
  357. [cluster-store-image]: https://img.shields.io/github/stars/coolaj86/cluster-store.svg?label=%E2%98%85
  358. [![★][connect-azuretables-image] connect-azuretables][connect-azuretables-url] An [Azure Table Storage](https://azure.microsoft.com/en-gb/services/storage/tables/)-based session store.
  359. [connect-azuretables-url]: https://www.npmjs.com/package/connect-azuretables
  360. [connect-azuretables-image]: https://img.shields.io/github/stars/mike-goodwin/connect-azuretables.svg?label=%E2%98%85
  361. [![★][connect-cloudant-store-image] connect-cloudant-store][connect-cloudant-store-url] An [IBM Cloudant](https://cloudant.com/)-based session store.
  362. [connect-cloudant-store-url]: https://www.npmjs.com/package/connect-cloudant-store
  363. [connect-cloudant-store-image]: https://img.shields.io/github/stars/adriantanasa/connect-cloudant-store.svg?label=%E2%98%85
  364. [![★][connect-couchbase-image] connect-couchbase][connect-couchbase-url] A [couchbase](http://www.couchbase.com/)-based session store.
  365. [connect-couchbase-url]: https://www.npmjs.com/package/connect-couchbase
  366. [connect-couchbase-image]: https://img.shields.io/github/stars/christophermina/connect-couchbase.svg?label=%E2%98%85
  367. [![★][connect-datacache-image] connect-datacache][connect-datacache-url] An [IBM Bluemix Data Cache](http://www.ibm.com/cloud-computing/bluemix/)-based session store.
  368. [connect-datacache-url]: https://www.npmjs.com/package/connect-datacache
  369. [connect-datacache-image]: https://img.shields.io/github/stars/adriantanasa/connect-datacache.svg?label=%E2%98%85
  370. [![★][connect-db2-image] connect-db2][connect-db2-url] An IBM DB2-based session store built using [ibm_db](https://www.npmjs.com/package/ibm_db) module.
  371. [connect-db2-url]: https://www.npmjs.com/package/connect-db2
  372. [connect-db2-image]: https://img.shields.io/github/stars/wallali/connect-db2.svg?label=%E2%98%85
  373. [![★][connect-dynamodb-image] connect-dynamodb][connect-dynamodb-url] A DynamoDB-based session store.
  374. [connect-dynamodb-url]: https://github.com/ca98am79/connect-dynamodb
  375. [connect-dynamodb-image]: https://img.shields.io/github/stars/ca98am79/connect-dynamodb.svg?label=%E2%98%85
  376. [![★][connect-loki-image] connect-loki][connect-loki-url] A Loki.js-based session store.
  377. [connect-loki-url]: https://www.npmjs.com/package/connect-loki
  378. [connect-loki-image]: https://img.shields.io/github/stars/Requarks/connect-loki.svg?label=%E2%98%85
  379. [![★][connect-ml-image] connect-ml][connect-ml-url] A MarkLogic Server-based session store.
  380. [connect-ml-url]: https://www.npmjs.com/package/connect-ml
  381. [connect-ml-image]: https://img.shields.io/github/stars/bluetorch/connect-ml.svg?label=%E2%98%85
  382. [![★][connect-mssql-image] connect-mssql][connect-mssql-url] A SQL Server-based session store.
  383. [connect-mssql-url]: https://www.npmjs.com/package/connect-mssql
  384. [connect-mssql-image]: https://img.shields.io/github/stars/patriksimek/connect-mssql.svg?label=%E2%98%85
  385. [![★][connect-monetdb-image] connect-monetdb][connect-monetdb-url] A MonetDB-based session store.
  386. [connect-monetdb-url]: https://www.npmjs.com/package/connect-monetdb
  387. [connect-monetdb-image]: https://img.shields.io/github/stars/MonetDB/npm-connect-monetdb.svg?label=%E2%98%85
  388. [![★][connect-mongo-image] connect-mongo][connect-mongo-url] A MongoDB-based session store.
  389. [connect-mongo-url]: https://www.npmjs.com/package/connect-mongo
  390. [connect-mongo-image]: https://img.shields.io/github/stars/kcbanner/connect-mongo.svg?label=%E2%98%85
  391. [![★][connect-mongodb-session-image] connect-mongodb-session][connect-mongodb-session-url] Lightweight MongoDB-based session store built and maintained by MongoDB.
  392. [connect-mongodb-session-url]: https://www.npmjs.com/package/connect-mongodb-session
  393. [connect-mongodb-session-image]: https://img.shields.io/github/stars/mongodb-js/connect-mongodb-session.svg?label=%E2%98%85
  394. [![★][connect-pg-simple-image] connect-pg-simple][connect-pg-simple-url] A PostgreSQL-based session store.
  395. [connect-pg-simple-url]: https://www.npmjs.com/package/connect-pg-simple
  396. [connect-pg-simple-image]: https://img.shields.io/github/stars/voxpelli/node-connect-pg-simple.svg?label=%E2%98%85
  397. [![★][connect-redis-image] connect-redis][connect-redis-url] A Redis-based session store.
  398. [connect-redis-url]: https://www.npmjs.com/package/connect-redis
  399. [connect-redis-image]: https://img.shields.io/github/stars/tj/connect-redis.svg?label=%E2%98%85
  400. [![★][connect-memcached-image] connect-memcached][connect-memcached-url] A memcached-based session store.
  401. [connect-memcached-url]: https://www.npmjs.com/package/connect-memcached
  402. [connect-memcached-image]: https://img.shields.io/github/stars/balor/connect-memcached.svg?label=%E2%98%85
  403. [![★][connect-memjs-image] connect-memjs][connect-memjs-url] A memcached-based session store using
  404. [memjs](https://www.npmjs.com/package/memjs) as the memcached client.
  405. [connect-memjs-url]: https://www.npmjs.com/package/connect-memjs
  406. [connect-memjs-image]: https://img.shields.io/github/stars/liamdon/connect-memjs.svg?label=%E2%98%85
  407. [![★][connect-session-knex-image] connect-session-knex][connect-session-knex-url] A session store using
  408. [Knex.js](http://knexjs.org/), which is a SQL query builder for PostgreSQL, MySQL, MariaDB, SQLite3, and Oracle.
  409. [connect-session-knex-url]: https://www.npmjs.com/package/connect-session-knex
  410. [connect-session-knex-image]: https://img.shields.io/github/stars/llambda/connect-session-knex.svg?label=%E2%98%85
  411. [![★][connect-session-sequelize-image] connect-session-sequelize][connect-session-sequelize-url] A session store using
  412. [Sequelize.js](http://sequelizejs.com/), which is a Node.js / io.js ORM for PostgreSQL, MySQL, SQLite and MSSQL.
  413. [connect-session-sequelize-url]: https://www.npmjs.com/package/connect-session-sequelize
  414. [connect-session-sequelize-image]: https://img.shields.io/github/stars/mweibel/connect-session-sequelize.svg?label=%E2%98%85
  415. [![★][dynamodb-store-image] dynamodb-store][dynamodb-store-url] A DynamoDB-based session store.
  416. [dynamodb-store-url]: https://www.npmjs.com/package/dynamodb-store
  417. [dynamodb-store-image]: https://img.shields.io/github/stars/rafaelrpinto/dynamodb-store.svg?label=%E2%98%85
  418. [![★][express-mysql-session-image] express-mysql-session][express-mysql-session-url] A session store using native
  419. [MySQL](https://www.mysql.com/) via the [node-mysql](https://github.com/felixge/node-mysql) module.
  420. [express-mysql-session-url]: https://www.npmjs.com/package/express-mysql-session
  421. [express-mysql-session-image]: https://img.shields.io/github/stars/chill117/express-mysql-session.svg?label=%E2%98%85
  422. [![★][express-oracle-session-image] express-oracle-session][express-oracle-session-url] A session store using native
  423. [oracle](https://www.oracle.com/) via the [node-oracledb](https://www.npmjs.com/package/oracledb) module.
  424. [express-oracle-session-url]: https://www.npmjs.com/package/express-oracle-session
  425. [express-oracle-session-image]: https://img.shields.io/github/stars/slumber86/express-oracle-session.svg?label=%E2%98%85
  426. [![★][express-sessions-image] express-sessions][express-sessions-url]: A session store supporting both MongoDB and Redis.
  427. [express-sessions-url]: https://www.npmjs.com/package/express-sessions
  428. [express-sessions-image]: https://img.shields.io/github/stars/konteck/express-sessions.svg?label=%E2%98%85
  429. [![★][connect-sqlite3-image] connect-sqlite3][connect-sqlite3-url] A [SQLite3](https://github.com/mapbox/node-sqlite3) session store modeled after the TJ's `connect-redis` store.
  430. [connect-sqlite3-url]: https://www.npmjs.com/package/connect-sqlite3
  431. [connect-sqlite3-image]: https://img.shields.io/github/stars/rawberg/connect-sqlite3.svg?label=%E2%98%85
  432. [![★][documentdb-session-image] documentdb-session][documentdb-session-url] A session store for Microsoft Azure's [DocumentDB](https://azure.microsoft.com/en-us/services/documentdb/) NoSQL database service.
  433. [documentdb-session-url]: https://www.npmjs.com/package/documentdb-session
  434. [documentdb-session-image]: https://img.shields.io/github/stars/dwhieb/documentdb-session.svg?label=%E2%98%85
  435. [![★][express-nedb-session-image] express-nedb-session][express-nedb-session-url] A NeDB-based session store.
  436. [express-nedb-session-url]: https://www.npmjs.com/package/express-nedb-session
  437. [express-nedb-session-image]: https://img.shields.io/github/stars/louischatriot/express-nedb-session.svg?label=%E2%98%85
  438. [![★][express-session-cache-manager-image] express-session-cache-manager][express-session-cache-manager-url]
  439. A store that implements [cache-manager](https://www.npmjs.com/package/cache-manager), which supports
  440. a [variety of storage types](https://www.npmjs.com/package/cache-manager#store-engines).
  441. [express-session-cache-manager-url]: https://www.npmjs.com/package/express-session-cache-manager
  442. [express-session-cache-manager-image]: https://img.shields.io/github/stars/theogravity/express-session-cache-manager.svg?label=%E2%98%85
  443. [![★][express-session-level-image] express-session-level][express-session-level-url] A [LevelDB](https://github.com/Level/levelup) based session store.
  444. [express-session-level-url]: https://www.npmjs.com/package/express-session-level
  445. [express-session-level-image]: https://img.shields.io/github/stars/tgohn/express-session-level.svg?label=%E2%98%85
  446. [![★][express-etcd-image] express-etcd][express-etcd-url] An [etcd](https://github.com/stianeikeland/node-etcd) based session store.
  447. [express-etcd-url]: https://www.npmjs.com/package/express-etcd
  448. [express-etcd-image]: https://img.shields.io/github/stars/gildean/express-etcd.svg?label=%E2%98%85
  449. [![★][fortune-session-image] fortune-session][fortune-session-url] A [Fortune.js](https://github.com/fortunejs/fortune)
  450. based session store. Supports all backends supported by Fortune (MongoDB, Redis, Postgres, NeDB).
  451. [fortune-session-url]: https://www.npmjs.com/package/fortune-session
  452. [fortune-session-image]: https://img.shields.io/github/stars/aliceklipper/fortune-session.svg?label=%E2%98%85
  453. [![★][hazelcast-store-image] hazelcast-store][hazelcast-store-url] A Hazelcast-based session store built on the [Hazelcast Node Client](https://www.npmjs.com/package/hazelcast-client).
  454. [hazelcast-store-url]: https://www.npmjs.com/package/hazelcast-store
  455. [hazelcast-store-image]: https://img.shields.io/github/stars/jackspaniel/hazelcast-store.svg?label=%E2%98%85
  456. [![★][level-session-store-image] level-session-store][level-session-store-url] A LevelDB-based session store.
  457. [level-session-store-url]: https://www.npmjs.com/package/level-session-store
  458. [level-session-store-image]: https://img.shields.io/github/stars/scriptollc/level-session-store.svg?label=%E2%98%85
  459. [![★][medea-session-store-image] medea-session-store][medea-session-store-url] A Medea-based session store.
  460. [medea-session-store-url]: https://www.npmjs.com/package/medea-session-store
  461. [medea-session-store-image]: https://img.shields.io/github/stars/BenjaminVadant/medea-session-store.svg?label=%E2%98%85
  462. [![★][memorystore-image] memorystore][memorystore-url] A memory session store made for production.
  463. [memorystore-url]: https://www.npmjs.com/package/memorystore
  464. [memorystore-image]: https://img.shields.io/github/stars/roccomuso/memorystore.svg?label=%E2%98%85
  465. [![★][mssql-session-store-image] mssql-session-store][mssql-session-store-url] A SQL Server-based session store.
  466. [mssql-session-store-url]: https://www.npmjs.com/package/mssql-session-store
  467. [mssql-session-store-image]: https://img.shields.io/github/stars/jwathen/mssql-session-store.svg?label=%E2%98%85
  468. [![★][nedb-session-store-image] nedb-session-store][nedb-session-store-url] An alternate NeDB-based (either in-memory or file-persisted) session store.
  469. [nedb-session-store-url]: https://www.npmjs.com/package/nedb-session-store
  470. [nedb-session-store-image]: https://img.shields.io/github/stars/JamesMGreene/nedb-session-store.svg?label=%E2%98%85
  471. [![★][sequelstore-connect-image] sequelstore-connect][sequelstore-connect-url] A session store using [Sequelize.js](http://sequelizejs.com/).
  472. [sequelstore-connect-url]: https://www.npmjs.com/package/sequelstore-connect
  473. [sequelstore-connect-image]: https://img.shields.io/github/stars/MattMcFarland/sequelstore-connect.svg?label=%E2%98%85
  474. [![★][session-file-store-image] session-file-store][session-file-store-url] A file system-based session store.
  475. [session-file-store-url]: https://www.npmjs.com/package/session-file-store
  476. [session-file-store-image]: https://img.shields.io/github/stars/valery-barysok/session-file-store.svg?label=%E2%98%85
  477. [![★][session-rethinkdb-image] session-rethinkdb][session-rethinkdb-url] A [RethinkDB](http://rethinkdb.com/)-based session store.
  478. [session-rethinkdb-url]: https://www.npmjs.com/package/session-rethinkdb
  479. [session-rethinkdb-image]: https://img.shields.io/github/stars/llambda/session-rethinkdb.svg?label=%E2%98%85
  480. ## Example
  481. A simple example using `express-session` to store page views for a user.
  482. ```js
  483. var express = require('express')
  484. var parseurl = require('parseurl')
  485. var session = require('express-session')
  486. var app = express()
  487. app.use(session({
  488. secret: 'keyboard cat',
  489. resave: false,
  490. saveUninitialized: true
  491. }))
  492. app.use(function (req, res, next) {
  493. if (!req.session.views) {
  494. req.session.views = {}
  495. }
  496. // get the url pathname
  497. var pathname = parseurl(req).pathname
  498. // count the views
  499. req.session.views[pathname] = (req.session.views[pathname] || 0) + 1
  500. next()
  501. })
  502. app.get('/foo', function (req, res, next) {
  503. res.send('you viewed this page ' + req.session.views['/foo'] + ' times')
  504. })
  505. app.get('/bar', function (req, res, next) {
  506. res.send('you viewed this page ' + req.session.views['/bar'] + ' times')
  507. })
  508. ```
  509. ## License
  510. [MIT](LICENSE)
  511. [npm-image]: https://img.shields.io/npm/v/express-session.svg
  512. [npm-url]: https://npmjs.org/package/express-session
  513. [travis-image]: https://img.shields.io/travis/expressjs/session/master.svg
  514. [travis-url]: https://travis-ci.org/expressjs/session
  515. [coveralls-image]: https://img.shields.io/coveralls/expressjs/session/master.svg
  516. [coveralls-url]: https://coveralls.io/r/expressjs/session?branch=master
  517. [downloads-image]: https://img.shields.io/npm/dm/express-session.svg
  518. [downloads-url]: https://npmjs.org/package/express-session
  519. [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
  520. [gratipay-url]: https://gratipay.com/dougwilson/