@oxog/spark 1.1.1 → 1.2.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/CHANGELOG.md +632 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1068 -956
- package/src/core/context.js +1098 -830
- package/src/core/middleware.js +303 -217
- package/src/index.js +183 -280
- package/src/middleware/body-parser.js +482 -526
- package/src/middleware/cache.js +177 -170
- package/src/middleware/compression.js +177 -182
- package/src/middleware/cors.js +176 -95
- package/src/middleware/health.js +219 -218
- package/src/middleware/logger.js +195 -194
- package/src/middleware/metrics.js +339 -327
- package/src/middleware/rate-limit.js +367 -319
- package/src/middleware/security.js +442 -332
- package/src/middleware/session.js +647 -529
- package/src/middleware/static.js +404 -299
- package/src/router/layer.js +197 -117
- package/src/router/route.js +149 -123
- package/src/router/router.js +703 -755
- package/src/utils/async-handler.js +145 -147
- package/src/utils/context-pool.js +83 -82
- package/src/utils/regex-validator.js +187 -163
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -959
- package/dist/core/context.js +0 -833
- package/dist/core/middleware.js +0 -220
- package/dist/core/request.js +0 -335
- package/dist/core/response.js +0 -377
- package/dist/index.js +0 -280
- package/dist/middleware/body-parser.js +0 -528
- package/dist/middleware/cache.js +0 -173
- package/dist/middleware/compression.js +0 -185
- package/dist/middleware/cors.js +0 -97
- package/dist/middleware/health.js +0 -221
- package/dist/middleware/index.js +0 -23
- package/dist/middleware/logger.js +0 -197
- package/dist/middleware/metrics.js +0 -330
- package/dist/middleware/rate-limit.js +0 -322
- package/dist/middleware/security.js +0 -335
- package/dist/middleware/session.js +0 -532
- package/dist/middleware/static.js +0 -301
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -119
- package/dist/router/route.js +0 -125
- package/dist/router/router.js +0 -758
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -150
- package/dist/utils/cache.js +0 -443
- package/dist/utils/context-pool.js +0 -85
- package/dist/utils/error-types.js +0 -134
- package/dist/utils/http.js +0 -415
- package/dist/utils/regex-validator.js +0 -166
- package/src/core/request.js +0 -335
- package/src/core/response.js +0 -377
- package/src/middleware/index.js +0 -23
- package/src/utils/cache.js +0 -443
- package/src/utils/error-types.js +0 -134
- package/src/utils/http.js +0 -415
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Ersin Koç
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ersin Koç
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,242 +1,289 @@
|
|
|
1
|
-
# ⚡ Spark
|
|
2
|
-
|
|
3
|
-
A fast, lightweight, and zero-dependency Node.js web framework built for performance and simplicity.
|
|
4
|
-
|
|
5
|
-
[](https://badge.fury.io/js/@oxog/spark)
|
|
6
|
-
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[](https://nodejs.org/)
|
|
8
|
-
|
|
9
|
-
## 🚀 Features
|
|
10
|
-
|
|
11
|
-
- **Zero Dependencies** - No external dependencies, pure Node.js
|
|
12
|
-
- **High Performance** - Built for speed with optimized request handling
|
|
13
|
-
- **Middleware Support** - Express-like middleware system
|
|
14
|
-
- **Advanced Router** - Powerful routing with parameter support
|
|
15
|
-
- **Session Management** - Built-in session handling with auto-save
|
|
16
|
-
- **Security First** - CORS, CSRF, rate limiting, and security headers
|
|
17
|
-
- **TypeScript Support** - Full TypeScript definitions included
|
|
18
|
-
- **Memory Efficient** - Optimized for low memory usage
|
|
19
|
-
- **Easy to Use** - Simple, intuitive API
|
|
20
|
-
|
|
21
|
-
## 📦 Installation
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- **[
|
|
54
|
-
- **[
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
- **
|
|
84
|
-
- **
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
-
|
|
91
|
-
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
1
|
+
# ⚡ Spark
|
|
2
|
+
|
|
3
|
+
A fast, lightweight, and zero-dependency Node.js web framework built for performance and simplicity.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/@oxog/spark)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://nodejs.org/)
|
|
8
|
+
|
|
9
|
+
## 🚀 Features
|
|
10
|
+
|
|
11
|
+
- **Zero Dependencies** - No external dependencies, pure Node.js
|
|
12
|
+
- **High Performance** - Built for speed with optimized request handling
|
|
13
|
+
- **Middleware Support** - Express-like middleware system
|
|
14
|
+
- **Advanced Router** - Powerful routing with parameter support
|
|
15
|
+
- **Session Management** - Built-in session handling with auto-save
|
|
16
|
+
- **Security First** - CORS, CSRF, rate limiting, and security headers
|
|
17
|
+
- **TypeScript Support** - Full TypeScript definitions included
|
|
18
|
+
- **Memory Efficient** - Optimized for low memory usage
|
|
19
|
+
- **Easy to Use** - Simple, intuitive API
|
|
20
|
+
|
|
21
|
+
## 📦 Installation
|
|
22
|
+
|
|
23
|
+
### Prerequisites
|
|
24
|
+
|
|
25
|
+
- Node.js >= 14.0.0
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @oxog/spark
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 🌟 Quick Start
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
const { Spark } = require('@oxog/spark');
|
|
35
|
+
|
|
36
|
+
const app = new Spark({
|
|
37
|
+
port: 3000,
|
|
38
|
+
host: '0.0.0.0'
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
app.get('/', (ctx) => {
|
|
42
|
+
ctx.json({ message: 'Welcome to Spark!' });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
app.listen(() => {
|
|
46
|
+
console.log(`Server is running at http://localhost:3000`);
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 📚 Documentation
|
|
51
|
+
|
|
52
|
+
### 🎯 **Quick Links**
|
|
53
|
+
- **[📖 Complete Documentation](docs/README.md)** - Full documentation index
|
|
54
|
+
- **[🚀 Getting Started](docs/getting-started.md)** - Quick start guide
|
|
55
|
+
- **[📋 API Reference](docs/api-reference.md)** - Complete API documentation
|
|
56
|
+
- **[🔧 Middleware Guide](docs/middleware-guide.md)** - Comprehensive middleware guide
|
|
57
|
+
|
|
58
|
+
### 📈 **Learning Path**
|
|
59
|
+
- **[🟢 Beginner Level](docs/beginner/)** - Start here if you're new to Spark
|
|
60
|
+
- **[🟡 Intermediate Level](docs/intermediate/)** - Build sophisticated applications
|
|
61
|
+
- **[🔴 Expert Level](docs/expert/)** - Master advanced patterns and architecture
|
|
62
|
+
|
|
63
|
+
### 🛡️ **Production Ready**
|
|
64
|
+
- **[Security Best Practices](docs/security-best-practices.md)** - Security guidelines
|
|
65
|
+
- **[Deployment Guide](docs/deployment.md)** - Production deployment instructions
|
|
66
|
+
|
|
67
|
+
## 🔧 Core Components
|
|
68
|
+
|
|
69
|
+
### Application
|
|
70
|
+
The main Spark application class that handles server lifecycle, middleware, and routing.
|
|
71
|
+
|
|
72
|
+
### Router
|
|
73
|
+
Advanced routing system with support for parameters, middleware, and nested routes.
|
|
74
|
+
|
|
75
|
+
### Context
|
|
76
|
+
Request/response context object with helper methods for common operations.
|
|
77
|
+
|
|
78
|
+
### Middleware
|
|
79
|
+
Extensible middleware system with built-in middleware for common tasks.
|
|
80
|
+
|
|
81
|
+
## 🛠️ Built-in Middleware
|
|
82
|
+
|
|
83
|
+
- **Body Parser** - Parse JSON, form data, and text
|
|
84
|
+
- **CORS** - Cross-origin resource sharing
|
|
85
|
+
- **Session** - Session management with auto-save functionality
|
|
86
|
+
- **Security** - Security headers and protection
|
|
87
|
+
- **Rate Limiting** - Request rate limiting
|
|
88
|
+
- **Compression** - Response compression
|
|
89
|
+
- **Static Files** - Static file serving
|
|
90
|
+
- **Logger** - Request logging
|
|
91
|
+
- **Health Check** - Health monitoring endpoints
|
|
92
|
+
|
|
93
|
+
## 📊 Performance
|
|
94
|
+
|
|
95
|
+
Spark is designed for high performance with minimal overhead:
|
|
96
|
+
|
|
97
|
+
- **~4000 req/sec** on standard hardware
|
|
98
|
+
- **<1ms** average response time
|
|
99
|
+
- **<50MB** memory footprint
|
|
100
|
+
- **Zero** external dependencies
|
|
101
|
+
|
|
102
|
+
## 🏗️ Architecture
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
┌─────────────────┐
|
|
106
|
+
│ Application │
|
|
107
|
+
├─────────────────┤
|
|
108
|
+
│ Middleware │
|
|
109
|
+
├─────────────────┤
|
|
110
|
+
│ Router │
|
|
111
|
+
├─────────────────┤
|
|
112
|
+
│ Context │
|
|
113
|
+
├─────────────────┤
|
|
114
|
+
│ HTTP Server │
|
|
115
|
+
└─────────────────┘
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 🔒 Security
|
|
119
|
+
|
|
120
|
+
Security features are middleware you register with `app.use()`; constructor
|
|
121
|
+
options do not enable them.
|
|
122
|
+
|
|
123
|
+
```javascript
|
|
124
|
+
const { Spark, security, cors, rateLimit, bodyParser, session, static: serveStatic } = require('@oxog/spark');
|
|
125
|
+
|
|
126
|
+
const app = new Spark({ settings: { trustProxy: true } }); // behind a proxy
|
|
127
|
+
app.use(security()); // CSP, HSTS, X-Frame-Options, nosniff, ...
|
|
128
|
+
app.use(cors({ origin: ['https://app.example.com'] })); // whitelist; sends Vary: Origin
|
|
129
|
+
app.use(rateLimit({ max: 100, windowMs: 60000 })); // 429 + Retry-After per client IP
|
|
130
|
+
app.use(bodyParser({ limit: '1mb' })); // 413 above the limit, also for chunked/gzip bodies
|
|
131
|
+
app.use(session({ secret: process.env.SESSION_SECRET }));
|
|
132
|
+
app.use(security.csrf({ secret: process.env.CSRF_SECRET })); // per-client tokens
|
|
133
|
+
app.use(serveStatic('./public')); // blocks ../, %2e%2e%2f, null bytes, dotfiles
|
|
134
|
+
|
|
135
|
+
app.get('/form', (ctx) => ctx.json({ csrfToken: ctx.csrfToken() }));
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
What is enforced (each item is covered by tests that make real HTTP requests):
|
|
139
|
+
|
|
140
|
+
- **Headers**: `ctx.set()` and cookies reject CR/LF/NUL, so handlers cannot be tricked into header injection
|
|
141
|
+
- **Bodies**: size limits (Content-Length, chunked and decompressed), JSON depth limit, `__proto__` keys dropped
|
|
142
|
+
- **CSRF**: tokens are bound to the client (session or an HttpOnly SameSite=Strict cookie) and only accepted from the `x-csrf-token` header or `_csrf` body field
|
|
143
|
+
- **Redirects**: `ctx.redirect()` refuses other hosts (including `//host` and `/\host`) unless `settings.allowedRedirectDomains` lists them
|
|
144
|
+
- **Errors**: 5xx messages and stack traces are only sent when `NODE_ENV=development`
|
|
145
|
+
- **XSS**: `security.xssProtection()` escapes `ctx.html()` and JSON string values
|
|
146
|
+
|
|
147
|
+
## 🔄 Streaming and Shutdown
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
// Stream a response with backpressure (the source is destroyed if the client disconnects)
|
|
151
|
+
app.get('/export', (ctx) => {
|
|
152
|
+
ctx.set('Content-Type', 'text/csv');
|
|
153
|
+
return ctx.stream(fs.createReadStream('export.csv'));
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Stop accepting connections, finish in-flight requests, close idle keep-alive
|
|
157
|
+
// sockets, and destroy whatever is still open after `shutdownTimeout` ms (default 10000)
|
|
158
|
+
await app.close();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
While listening, the app closes gracefully on SIGTERM/SIGINT (`handleSignals: false`
|
|
162
|
+
to opt out) and exits with code 1 after an uncaught exception
|
|
163
|
+
(`exitOnUncaughtException: false` to opt out). These process listeners are removed by `close()`.
|
|
164
|
+
|
|
165
|
+
## 🧪 Testing
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
npm test # test suite (real HTTP servers on ephemeral ports)
|
|
169
|
+
npm run test:coverage # with enforced coverage thresholds
|
|
170
|
+
npm run check # lint + typecheck + coverage
|
|
171
|
+
npm run test:package # pack, install and smoke-test the published archive
|
|
172
|
+
npm run benchmark # performance benchmark
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## 📈 Examples
|
|
176
|
+
|
|
177
|
+
### Basic API
|
|
178
|
+
```javascript
|
|
179
|
+
const { Spark } = require('@oxog/spark');
|
|
180
|
+
|
|
181
|
+
const app = new Spark();
|
|
182
|
+
|
|
183
|
+
app.get('/users', (ctx) => {
|
|
184
|
+
ctx.json([
|
|
185
|
+
{ id: 1, name: 'John Doe' },
|
|
186
|
+
{ id: 2, name: 'Jane Smith' }
|
|
187
|
+
]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
app.get('/users/:id', (ctx) => {
|
|
191
|
+
const { id } = ctx.params;
|
|
192
|
+
ctx.json({ id, name: 'User ' + id });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
app.listen(3000);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### With Middleware
|
|
199
|
+
```javascript
|
|
200
|
+
const { Spark, middleware } = require('@oxog/spark');
|
|
201
|
+
|
|
202
|
+
const app = new Spark();
|
|
203
|
+
|
|
204
|
+
// Global middleware
|
|
205
|
+
app.use(middleware.logger());
|
|
206
|
+
app.use(middleware.cors({
|
|
207
|
+
origin: 'https://example.com'
|
|
208
|
+
}));
|
|
209
|
+
|
|
210
|
+
// Route-specific middleware
|
|
211
|
+
const auth = (ctx, next) => {
|
|
212
|
+
const token = ctx.get('Authorization');
|
|
213
|
+
if (!token || token !== 'Bearer valid-token') {
|
|
214
|
+
return ctx.status(401).json({ error: 'Unauthorized' });
|
|
215
|
+
}
|
|
216
|
+
return next();
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
app.get('/protected', auth, (ctx) => {
|
|
220
|
+
ctx.json({ message: 'This is a protected route.' });
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
app.listen(3000);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### E-commerce API with Sessions
|
|
227
|
+
```javascript
|
|
228
|
+
const { Spark, Router, bodyParser, session } = require('@oxog/spark');
|
|
229
|
+
|
|
230
|
+
const app = new Spark();
|
|
231
|
+
const api = new Router();
|
|
232
|
+
|
|
233
|
+
app.use(bodyParser());
|
|
234
|
+
|
|
235
|
+
// Session middleware with auto-save
|
|
236
|
+
app.use(session({
|
|
237
|
+
secret: 'your-secret-key',
|
|
238
|
+
saveUninitialized: true,
|
|
239
|
+
cookie: {
|
|
240
|
+
maxAge: 24 * 60 * 60 * 1000, // 24 hours (milliseconds)
|
|
241
|
+
httpOnly: true,
|
|
242
|
+
secure: process.env.NODE_ENV === 'production'
|
|
243
|
+
}
|
|
244
|
+
}));
|
|
245
|
+
|
|
246
|
+
// Authentication
|
|
247
|
+
api.post('/auth/login', (ctx) => {
|
|
248
|
+
const { email, password } = ctx.body;
|
|
249
|
+
|
|
250
|
+
if (authenticate(email, password)) {
|
|
251
|
+
ctx.session.userId = user.id; // Auto-saved immediately
|
|
252
|
+
ctx.json({ success: true });
|
|
253
|
+
} else {
|
|
254
|
+
ctx.status(401).json({ error: 'Invalid credentials' });
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// Protected routes
|
|
259
|
+
api.get('/orders', requireAuth, (ctx) => {
|
|
260
|
+
const orders = getOrdersByUser(ctx.session.userId);
|
|
261
|
+
ctx.json({ orders });
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
app.use('/api', api.routes());
|
|
265
|
+
app.listen(3000);
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## 🤝 Contributing
|
|
269
|
+
|
|
270
|
+
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
271
|
+
|
|
272
|
+
## 📄 License
|
|
273
|
+
|
|
274
|
+
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
275
|
+
|
|
276
|
+
## 🔗 Links
|
|
277
|
+
|
|
278
|
+
- [Documentation](docs/)
|
|
279
|
+
- [Examples](examples/)
|
|
280
|
+
- [Issues](https://github.com/ersinkoc/Spark/issues)
|
|
281
|
+
- [Changelog](CHANGELOG.md)
|
|
282
|
+
|
|
283
|
+
## 🌟 Support
|
|
284
|
+
|
|
285
|
+
If you find Spark useful, please consider giving it a star on GitHub!
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
242
289
|
Built with ❤️ by the Spark team
|