@increase21/simplenodejs 1.0.11 → 1.0.12
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 +275 -0
- package/package.json +1 -1
- package/readme.md +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# @increase21/simplenodejs
|
|
2
|
+
|
|
3
|
+
**SimpleNodeJS** is a minimal, dependency-free Node.js framework built on top of Node’s native `http` and `https` module.
|
|
4
|
+
It provides controller-based routing, middleware, plugins, and security utilities with full TypeScript support.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ✨ Features
|
|
9
|
+
|
|
10
|
+
- Native Node.js HTTP server (no Express/Fastify)
|
|
11
|
+
- Controller-based routing (file-system driven)
|
|
12
|
+
- Middleware & error middleware
|
|
13
|
+
- Plugin system
|
|
14
|
+
- Built-in security middlewares
|
|
15
|
+
- Rate limiting
|
|
16
|
+
- CORS
|
|
17
|
+
- body and Query parsing
|
|
18
|
+
- HTML responses
|
|
19
|
+
- TypeScript-first
|
|
20
|
+
- Reverse-proxy friendly (Nginx)
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 📦 Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @increase21/simplenodejs
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 🚀 Quick Start
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { CreateSimpleJsHttpServer } from "@increase21/simplenodejs";
|
|
36
|
+
|
|
37
|
+
const app = CreateSimpleJsHttpServer({
|
|
38
|
+
controllersDir: process.cwd()+ "/controllers",
|
|
39
|
+
trustProxy: true
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
app.listen(3000, () => {
|
|
43
|
+
console.log("Server running on http://localhost:3000");
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## ⚙️ CreateSimpleJsHttpServer(options)
|
|
50
|
+
|
|
51
|
+
Creates and returns the HTTP app instance.
|
|
52
|
+
|
|
53
|
+
### Parameters
|
|
54
|
+
|
|
55
|
+
| Param | Type | Required | Description |
|
|
56
|
+
|------|------|----------|-------------|
|
|
57
|
+
| `controllersDir` | `string` | ✅ | Absolute path to your controllers directory |
|
|
58
|
+
<!-- | `trustProxy` | `boolean` | ❌ | If `true`, uses `x-forwarded-for` for IP detection (for Nginx/load balancers) |
|
|
59
|
+
| `globalPrefix` | `string` | ❌ | Prefix all routes, e.g. `/api` | -->
|
|
60
|
+
|
|
61
|
+
### Example
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const app = CreateSimpleJsHttpServer({
|
|
65
|
+
controllersDir: process.cwd()+ "/controllers",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
app.listen(4000,callback)
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 📁 Controllers
|
|
75
|
+
|
|
76
|
+
Controllers are auto-loaded from `controllersDir`.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
export default AuthControllers extends SimpleNodeJsController {
|
|
80
|
+
|
|
81
|
+
async login(id:string) {
|
|
82
|
+
return this.RunRequest({
|
|
83
|
+
post: //....your post method handler,
|
|
84
|
+
get://...
|
|
85
|
+
put://...
|
|
86
|
+
delete://....
|
|
87
|
+
...
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Controller Object Params
|
|
94
|
+
Each method defined in a controller file is exposed as an endpoint by SimpleNodeJsController.
|
|
95
|
+
Methods can receive parameters, which are passed through the URL pathname. When using this.RunRequest(...), the handler receives SimpleJsPrivateMethodProps.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 🧾 SimpleJsPrivateMethodProps
|
|
100
|
+
```ts
|
|
101
|
+
{
|
|
102
|
+
body: any // parsed payload.
|
|
103
|
+
res: ResponseObject;
|
|
104
|
+
req: RequestObject;
|
|
105
|
+
query: JSON // parsed requests param.
|
|
106
|
+
id?: string;
|
|
107
|
+
customData?: any //any custom data attached to req._custom_data by middlewares
|
|
108
|
+
idMethod?: {
|
|
109
|
+
post?: 'required' | 'optional',
|
|
110
|
+
get?: 'required' | 'optional',
|
|
111
|
+
put?: 'required' | 'optional',
|
|
112
|
+
delete?: 'required' | 'optional',
|
|
113
|
+
patch?: 'required' | 'optional',
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 🧾 RequestObject (req)
|
|
119
|
+
|
|
120
|
+
Available on every route handler.
|
|
121
|
+
|
|
122
|
+
| Property | Type | Description |
|
|
123
|
+
|---------|------|-------------|
|
|
124
|
+
| `req.url` | `string` | Request URL |
|
|
125
|
+
| `req.method` | `string` | HTTP method |
|
|
126
|
+
| `req.query` | `object` | Parsed query params |
|
|
127
|
+
| `req.body` | `any` | Parsed request body |
|
|
128
|
+
| `req.raw_body` | `any` | Parsed request body |
|
|
129
|
+
| `req._custom_data` | `any` |
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 🧾 ResponseObject (res)
|
|
134
|
+
|
|
135
|
+
| Method | Params | Description |
|
|
136
|
+
|--------|--------|-------------|
|
|
137
|
+
| `res.status(code)` | `number` | Set HTTP status |
|
|
138
|
+
| `res.json(data)` | `any` | Send JSON response |
|
|
139
|
+
| `res.text(data)` | `string | Buffer` | Send raw response |
|
|
140
|
+
| `res.html(html)` | `string` | Send HTML response |
|
|
141
|
+
|
|
142
|
+
### Example
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
res.status(200).json({ success: true });
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 🔌 app.use(middleware)
|
|
151
|
+
|
|
152
|
+
Registers a middleware that runs before controllers.
|
|
153
|
+
|
|
154
|
+
### Middleware Signature
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
(req: RequestObject, res: ResponseObject, next: () => Promise<void> | void) => Promise<void> | void
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Example
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
app.use(async (req, res, next) => {
|
|
164
|
+
console.log(req.method, req.url);
|
|
165
|
+
await next();
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## ❌ app.useError(errorMiddleware)
|
|
172
|
+
|
|
173
|
+
Registers a global error handler.
|
|
174
|
+
|
|
175
|
+
### Signature
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
(err: any, req: RequestObject, res: ResponseObject, next: () => void) => void
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## 🧩 app.registerPlugin(app=>plugin(app,opts))
|
|
184
|
+
|
|
185
|
+
Registers a plugin.
|
|
186
|
+
|
|
187
|
+
### Plugin Shape
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
type Plugin = (app: SimpleJsServer, opts?: any) => void | Promise<void>;
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Built-In Plugins
|
|
194
|
+
|
|
195
|
+
| name | Description |
|
|
196
|
+
|-----------|-------------|
|
|
197
|
+
| `SimpleJsSecurityPlugin` | CORS, RateLimit, Helmet |
|
|
198
|
+
| `SimpleJsJWTPlugin` | JWT protection|
|
|
199
|
+
| `SimpleJsIPWhitelistPlugin` | Restricting IP addresses |
|
|
200
|
+
| `SimpleJsCookiePlugin` | Restricting IP addresses |
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
# 🧱 Built-in Middlewares
|
|
205
|
+
|
|
206
|
+
## 🔐 SetRequestCORS(options?)
|
|
207
|
+
|
|
208
|
+
Adds security headers.
|
|
209
|
+
|
|
210
|
+
### Options (optional)
|
|
211
|
+
All the standard http headers
|
|
212
|
+
|
|
213
|
+
### Usage
|
|
214
|
+
```ts
|
|
215
|
+
app.use(app=>SetRequestCORS(app, [{
|
|
216
|
+
"Access-Control-Allow-Origin": "*",
|
|
217
|
+
"X-Frame-Options": "DENY",
|
|
218
|
+
}]));
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## ⏱ SetRateLimiter(options)
|
|
224
|
+
|
|
225
|
+
### Options
|
|
226
|
+
|
|
227
|
+
| Param | Type | Required | Description |
|
|
228
|
+
|------|------|----------|-------------|
|
|
229
|
+
| `windowMs` | `number` | ✅ | Time window in ms |
|
|
230
|
+
| `max` | `number` | ✅ | Max requests per window |
|
|
231
|
+
| `keyGenerator` | `(req) => string` | ❌ | Custom key generator |
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
app.use(SetRateLimiter({
|
|
235
|
+
windowMs: 60_000,
|
|
236
|
+
max: 100,
|
|
237
|
+
keyGenerator: (req) => req.ip
|
|
238
|
+
}));
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## 📥 SetBodyParser(options?)
|
|
244
|
+
SetBodyParser middleware must be set for controllers to receive all needed data to process.
|
|
245
|
+
### Options
|
|
246
|
+
|
|
247
|
+
| Param | Type | Description |
|
|
248
|
+
|------|------|-------------|
|
|
249
|
+
| `limit` | `string` or `number` | Max body size (e.g. "1mb")
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
app.use(SetBodyParser({ limit: "2mb" }));
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## 🛡 Security Best Practices
|
|
258
|
+
|
|
259
|
+
- Always enable `SetSecurityHeaders`
|
|
260
|
+
- Enable `SetRateLimiter` on public APIs
|
|
261
|
+
- Validate request body
|
|
262
|
+
- Use `trustProxy: true` only behind trusted proxies
|
|
263
|
+
- Avoid leaking stack traces in production
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## 📄 License
|
|
268
|
+
|
|
269
|
+
MIT
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## 👤 Author
|
|
274
|
+
|
|
275
|
+
Increase
|
package/package.json
CHANGED
package/readme.md
DELETED
|
File without changes
|