프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / express-session / README.md

이력 | 보기 | 이력해설 | 다운로드 (31.4 KB)

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