You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[connect-mongo](https://github.com/jdesboeufs/connect-mongo) package stores MongoDB session for Express and Connect
1
+
### [connect-mongo](https://github.com/jdesboeufs/connect-mongo) package stores MongoDB session for Express and Connect
2
+
3
+
So, implemented sessions using Passport, but for storing sessions into my mongodb I use connect-mongo using a mongoose connection to connect to the mongodb database.
4
+
5
+
Then most standard implementation code is given in the [officila doc](https://github.com/jdesboeufs/connect-mongo#express-or-connect-integration)
connect-mongo stores sessions in the ["sessions" collection by default](https://github.com/jdesboeufs/connect-mongo/blob/bca754cc6ccded953f85ca37f647619f26b6783d/lib/connect-mongo.js#L22).
Copy file name to clipboardExpand all lines: Node-Express/express-session-how-it-works.md
+57-5Lines changed: 57 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,43 @@
1
+
## What is a session?
2
+
3
+
A session is a place to store data that you want access to across requests. Each user that visits your website has a unique session. You can use sessions to store and access user data as they browse your application. Sessions are integral to web application development because they allow the application to store state. Based on what action a user took on Page A, we can show a different Page B. Without them, applications would be stateless, and not very useful.
4
+
5
+
Sessions can store their information in different ways. The popular ways to store session data is:
6
+
7
+
- In application memory
8
+
- In a cookie
9
+
- In a memory cache
10
+
- In a database
11
+
12
+
#### The module like express-session will provide you with a nice API to work with sessions (letting you get & set data to the session), but under the hood, it will save and retrieve this data using a cookie.
13
+
14
+
15
+
### Storing Session Data in Application Memory
16
+
17
+
One way to store session data is in Application memory. This is often the simplest way, but not used in production.
18
+
19
+
Storing session data in application memory essentially means that the data is stored for the lifetime of your application runtime. If your web application server crashes or is stopped, all session data is removed.
20
+
21
+
Storing session data in memory also causes memory leaks. As your application stays running, more and more memory is used, until your app runs out of memory.
22
+
23
+
For development purposes, it is often useful to store sessions in application memory. Otherwise, there are better ways of storing session data. We’ll explore these below.
24
+
25
+
### Storing Session Data in Cookies
26
+
27
+
A cookie is usually a small piece of data that gets sent between a web server to your web browser. It allows the server to store information relevant to a specific user.
28
+
29
+
One common use for cookies is to store session data. This works in the following way.
30
+
31
+
The server issues a cookie that gets sent to the web browser and stored for a period of time (called the expiration time).
32
+
When a user makes a subsequent request to the web server, this cookie gets sent along with the request, and the server can read the information that is in it.
33
+
The server can manipulate the cookie if it needs to, and then sends it back to the browser.
34
+
Until the cookie expires, every time you make a request, your browser will send the cookies back to the server.
35
+
36
+
#### The module like express-session will provide you with a nice API to work with sessions (letting you get & set data to the session), but under the hood, it will save and retrieve this data using a cookie.
37
+
38
+
39
+
40
+
1
41
### We use sessions to maintain state between user requests and we use cookies to transport the session ID between those requests.
2
42
3
43
Every user interaction with your application is an isolated and individual request and response. The need to persist information between requests is vital for maintaining the ultimate experience for the user.
@@ -8,12 +48,26 @@ So I have to securely set up sessions in my application to mitigate risks such a
8
48
9
49
[express-session](https://www.npmjs.com/package/express-session) (https://github.com/expressjs/session ) - A very popular session module that has been highly vetted by the community and constantly improved.
10
50
51
+
```js
52
+
constsession=require('express-session')
53
+
constdbConnection=require('./database')
54
+
constMongoStore=require('connect-mongo')(session)
11
55
56
+
app.use(
57
+
session({
58
+
secret:'fraggle-rock', //pick a random string to make the hash that is generated secure
We're importing the [session function](https://github.com/expressjs/session/blob/master/session/session.js#L24) from the express-session NPM module and passing the session function a configuration object to set properties such as:
69
+
We're importing the [session function](https://github.com/expressjs/session/blob/master/session/session.js#L24) from the express-session NPM module and passing the session function a configuration object to set properties inside the object passed to express-session. Note **express-session**, requires an object as an argument to initialize it.
70
+
17
71
18
72
**Secret**. Required option. This is a value used in the signing of the session ID cookie, that is stored in the cookie.
19
73
@@ -37,9 +91,7 @@ Forces a session that is "uninitialized" to be saved to the store. A session is
0 commit comments