expressjs-session 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +207 -0
- package/icons/icon.png +0 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# expressjs-session
|
|
2
|
+
|
|
3
|
+
Lightweight, secure session middleware for Express.js applications.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Table of Contents
|
|
12
|
+
|
|
13
|
+
- [Introduction](#introduction)
|
|
14
|
+
- [Features](#features)
|
|
15
|
+
- [Installation](#installation)
|
|
16
|
+
- [Quick Start](#quick-start)
|
|
17
|
+
- [API](#api)
|
|
18
|
+
- [Configuration Options](#configuration-options)
|
|
19
|
+
- [Examples](#examples)
|
|
20
|
+
- [Error Handling](#error-handling)
|
|
21
|
+
- [Security Considerations](#security-considerations)
|
|
22
|
+
- [Contributing](#contributing)
|
|
23
|
+
- [License](#license)
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Introduction
|
|
28
|
+
|
|
29
|
+
`expressjs-session` provides a simple, pluggable session middleware for Express applications. It stores session data server-side (in memory, Redis, MongoDB, etc.) and manages a secure cookie containing the session ID. Think of it as the receptionist at a hotel: it hands out room keys (session IDs) and looks up your data when you come back.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Features
|
|
34
|
+
|
|
35
|
+
- **Simple API** — plug into any Express app in one line
|
|
36
|
+
- **Multiple stores** — in-memory, Redis, MongoDB, and custom stores
|
|
37
|
+
- **Secure cookies** — HTTP-only, signed, optional encryption
|
|
38
|
+
- **Automatic rotation** — prevent fixation attacks by regenerating IDs
|
|
39
|
+
- **Built-in TTL** — sessions expire automatically after configured timeout
|
|
40
|
+
- **Promise support** — async/await–friendly store interface
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install expressjs-session
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
const express = require('express');
|
|
56
|
+
const session = require('expressjs-session');
|
|
57
|
+
|
|
58
|
+
const app = express();
|
|
59
|
+
|
|
60
|
+
app.use(session({
|
|
61
|
+
secret: 'keyboard cat',
|
|
62
|
+
resave: false,
|
|
63
|
+
saveUninitialized: true,
|
|
64
|
+
cookie: { maxAge: 60000 }
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
app.get('/', (req, res) => {
|
|
68
|
+
if (req.session.views) {
|
|
69
|
+
req.session.views++;
|
|
70
|
+
res.send(`Welcome back! You’ve visited ${req.session.views} times.`);
|
|
71
|
+
} else {
|
|
72
|
+
req.session.views = 1;
|
|
73
|
+
res.send('Hello, first time visitor!');
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
app.listen(3000, () => {
|
|
78
|
+
console.log('Server running on http://localhost:3000');
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## API
|
|
85
|
+
|
|
86
|
+
### `session(options)`
|
|
87
|
+
|
|
88
|
+
Returns an Express middleware function.
|
|
89
|
+
|
|
90
|
+
- **options.secret** _(string, required)_
|
|
91
|
+
A string (or array of strings) used to sign the session ID cookie.
|
|
92
|
+
|
|
93
|
+
- **options.store** _(Store instance, optional)_
|
|
94
|
+
A compatible session store (default: in-memory).
|
|
95
|
+
|
|
96
|
+
- **options.resave** _(boolean, default: true)_
|
|
97
|
+
Forces the session to be saved back to the session store, even if it wasn’t modified.
|
|
98
|
+
|
|
99
|
+
- **options.saveUninitialized** _(boolean, default: true)_
|
|
100
|
+
Forces a session that is “uninitialized” to be saved to the store.
|
|
101
|
+
|
|
102
|
+
- **options.cookie** _(object, optional)_
|
|
103
|
+
Cookie settings (path, domain, maxAge, secure, httpOnly, sameSite).
|
|
104
|
+
|
|
105
|
+
- **options.name** _(string, default: `connect.sid`)_
|
|
106
|
+
Name of the session ID cookie.
|
|
107
|
+
|
|
108
|
+
- **options.genid** _(function, optional)_
|
|
109
|
+
Custom function to generate session IDs.
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Configuration Options
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
{
|
|
117
|
+
secret: 'your secret',
|
|
118
|
+
name: 'sessionId',
|
|
119
|
+
resave: false,
|
|
120
|
+
saveUninitialized: false,
|
|
121
|
+
cookie: {
|
|
122
|
+
maxAge: 24 * 60 * 60 * 1000, // 1 day
|
|
123
|
+
secure: true,
|
|
124
|
+
httpOnly: true,
|
|
125
|
+
sameSite: 'lax'
|
|
126
|
+
},
|
|
127
|
+
store: new RedisStore({ /* ... */ }),
|
|
128
|
+
genid: () => crypto.randomUUID()
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## Examples
|
|
135
|
+
|
|
136
|
+
### Using Redis as a Session Store
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
const Redis = require('ioredis');
|
|
140
|
+
const RedisStore = require('expressjs-session').RedisStore;
|
|
141
|
+
|
|
142
|
+
app.use(session({
|
|
143
|
+
secret: 'keyboard cat',
|
|
144
|
+
store: new RedisStore({
|
|
145
|
+
client: new Redis(),
|
|
146
|
+
ttl: 86400 // 1 day
|
|
147
|
+
}),
|
|
148
|
+
cookie: { secure: true }
|
|
149
|
+
}));
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Regenerating a Session
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
app.post('/login', (req, res, next) => {
|
|
156
|
+
// Authenticate user...
|
|
157
|
+
req.session.regenerate(err => {
|
|
158
|
+
if (err) return next(err);
|
|
159
|
+
req.session.userId = user.id;
|
|
160
|
+
res.redirect('/dashboard');
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Error Handling
|
|
168
|
+
|
|
169
|
+
If your store emits errors, they’ll be passed to `next(err)` in Express:
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
app.use((err, req, res, next) => {
|
|
173
|
+
console.error('Session error:', err);
|
|
174
|
+
res.status(500).send('Internal Server Error');
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Security Considerations
|
|
181
|
+
|
|
182
|
+
- **Rotate secrets** regularly and use a strong, unguessable string.
|
|
183
|
+
- **Use HTTPS** so cookies marked `secure` aren’t sent over plain HTTP.
|
|
184
|
+
- **Limit cookie scope** via `domain`, `path`, and `sameSite` as needed.
|
|
185
|
+
- **Avoid memory store** in production—it does not scale and leaks memory.
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Contributing
|
|
190
|
+
|
|
191
|
+
1. Fork the repo
|
|
192
|
+
2. Create your feature branch (`git checkout -b feature/foo`)
|
|
193
|
+
3. Commit your changes (`git commit -am 'Add foo'`)
|
|
194
|
+
4. Push to the branch (`git push origin feature/foo`)
|
|
195
|
+
5. Open a Pull Request
|
|
196
|
+
|
|
197
|
+
Please run tests with:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
npm test
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
[MIT](LICENSE)
|
package/icons/icon.png
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expressjs-session",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "This is a Node.js module available through the npm registry",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
|
-
"url": "https://github.com/expressjs/
|
|
10
|
+
"url": "https://github.com/expressjs/session"
|
|
11
11
|
},
|
|
12
|
+
"icon": "icons/icon.png",
|
|
12
13
|
"author": "npm",
|
|
13
14
|
"license": "ISC"
|
|
14
15
|
}
|