@increase21/simplenodejs 1.0.11 → 1.0.13

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.
Files changed (3) hide show
  1. package/README.md +291 -0
  2. package/package.json +1 -1
  3. package/readme.md +0 -0
package/README.md ADDED
@@ -0,0 +1,291 @@
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
+ #### Running Multiple Methods
78
+ ./controllers/{servicefolder}/auth.ts
79
+ ./controllers/{servicefolder}/auth.js
80
+
81
+ ```ts
82
+ export default AuthControllers extends SimpleNodeJsController {
83
+
84
+ async account(id:string) {
85
+ return this.RunRequest({
86
+ post: //....your post method handler,
87
+ get://...
88
+ put://...
89
+ delete://....
90
+ ...
91
+ })
92
+ }
93
+ };
94
+ ```
95
+ Available on POST|GET|PUT|DELETE at http://baseURl/{servicefolder}/auth/account
96
+
97
+ #### Running Single Method
98
+ ```ts
99
+ export default AuthControllers extends SimpleNodeJsController {
100
+
101
+ async login() {
102
+ if(this.method !=="post") return this.res.status(405).json({error:"Method Not Allowed"})
103
+
104
+ return YourHandler(SimpleJsPrivateMethodProps)
105
+ }
106
+ };
107
+ ```
108
+
109
+ ### Controller Object Params
110
+ Each method defined in a controller file is exposed as an endpoint by SimpleNodeJsController.
111
+ Methods can receive parameters, which are passed through the URL pathname. When using this.RunRequest(...), the handler receives SimpleJsPrivateMethodProps.
112
+
113
+ ---
114
+
115
+ ## 🧾 SimpleJsPrivateMethodProps
116
+ ```ts
117
+ {
118
+ body: any // parsed payload.
119
+ res: ResponseObject;
120
+ req: RequestObject;
121
+ query: JSON // parsed requests param.
122
+ id?: string;
123
+ customData?: any //any custom data attached to req._custom_data by middlewares
124
+ idMethod?: {
125
+ post?: 'required' | 'optional',
126
+ get?: 'required' | 'optional',
127
+ put?: 'required' | 'optional',
128
+ delete?: 'required' | 'optional',
129
+ patch?: 'required' | 'optional',
130
+ }
131
+ }
132
+ ```
133
+
134
+ ## 🧾 RequestObject (req)
135
+
136
+ Available on every route handler.
137
+
138
+ | Property | Type | Description |
139
+ |---------|------|-------------|
140
+ | `req.url` | `string` | Request URL |
141
+ | `req.method` | `string` | HTTP method |
142
+ | `req.query` | `object` | Parsed query params |
143
+ | `req.body` | `any` | Parsed request body |
144
+ | `req.raw_body` | `any` | Parsed request body |
145
+ | `req._custom_data` | `any` |
146
+
147
+ ---
148
+
149
+ ## 🧾 ResponseObject (res)
150
+
151
+ | Method | Params | Description |
152
+ |--------|--------|-------------|
153
+ | `res.status(code)` | `number` | Set HTTP status |
154
+ | `res.json(data)` | `any` | Send JSON response |
155
+ | `res.text(data)` | `string | Buffer` | Send raw response |
156
+ | `res.html(html)` | `string` | Send HTML response |
157
+
158
+ ### Example
159
+
160
+ ```ts
161
+ res.status(200).json({ success: true });
162
+ ```
163
+
164
+ ---
165
+
166
+ ## 🔌 app.use(middleware)
167
+
168
+ Registers a middleware that runs before controllers.
169
+
170
+ ### Middleware Signature
171
+
172
+ ```ts
173
+ (req: RequestObject, res: ResponseObject, next: () => Promise<void> | void) => Promise<void> | void
174
+ ```
175
+
176
+ ### Example
177
+
178
+ ```ts
179
+ app.use(async (req, res, next) => {
180
+ console.log(req.method, req.url);
181
+ await next();
182
+ });
183
+ ```
184
+
185
+ ---
186
+
187
+ ## ❌ app.useError(errorMiddleware)
188
+
189
+ Registers a global error handler.
190
+
191
+ ### Signature
192
+
193
+ ```ts
194
+ (err: any, req: RequestObject, res: ResponseObject, next: () => void) => void
195
+ ```
196
+
197
+ ---
198
+
199
+ ## 🧩 app.registerPlugin(app=>plugin(app,opts))
200
+
201
+ Registers a plugin.
202
+
203
+ ### Plugin Shape
204
+
205
+ ```ts
206
+ type Plugin = (app: SimpleJsServer, opts?: any) => void | Promise<void>;
207
+ ```
208
+
209
+ ### Built-In Plugins
210
+
211
+ | name | Description |
212
+ |-----------|-------------|
213
+ | `SimpleJsSecurityPlugin` | CORS, RateLimit, Helmet |
214
+ | `SimpleJsJWTPlugin` | JWT protection|
215
+ | `SimpleJsIPWhitelistPlugin` | Restricting IP addresses |
216
+ | `SimpleJsCookiePlugin` | Restricting IP addresses |
217
+
218
+ ---
219
+
220
+ # 🧱 Built-in Middlewares
221
+
222
+ ## 🔐 SetRequestCORS(options?)
223
+
224
+ Adds security headers.
225
+
226
+ ### Options (optional)
227
+ All the standard http headers
228
+
229
+ ### Usage
230
+ ```ts
231
+ app.use(app=>SetRequestCORS(app, [{
232
+ "Access-Control-Allow-Origin": "*",
233
+ "X-Frame-Options": "DENY",
234
+ }]));
235
+ ```
236
+
237
+ ---
238
+
239
+ ## ⏱ SetRateLimiter(options)
240
+
241
+ ### Options
242
+
243
+ | Param | Type | Required | Description |
244
+ |------|------|----------|-------------|
245
+ | `windowMs` | `number` | ✅ | Time window in ms |
246
+ | `max` | `number` | ✅ | Max requests per window |
247
+ | `keyGenerator` | `(req) => string` | ❌ | Custom key generator |
248
+
249
+ ```ts
250
+ app.use(SetRateLimiter({
251
+ windowMs: 60_000,
252
+ max: 100,
253
+ keyGenerator: (req) => req.ip
254
+ }));
255
+ ```
256
+
257
+ ---
258
+
259
+ ## 📥 SetBodyParser(options?)
260
+ SetBodyParser middleware must be set for controllers to receive all needed data to process.
261
+ ### Options
262
+
263
+ | Param | Type | Description |
264
+ |------|------|-------------|
265
+ | `limit` | `string` or `number` | Max body size (e.g. "1mb")
266
+
267
+ ```ts
268
+ app.use(SetBodyParser({ limit: "2mb" }));
269
+ ```
270
+
271
+ ---
272
+
273
+ ## 🛡 Security Best Practices
274
+
275
+ - Always enable `SetSecurityHeaders`
276
+ - Enable `SetRateLimiter` on public APIs
277
+ - Validate request body
278
+ - Use `trustProxy: true` only behind trusted proxies
279
+ - Avoid leaking stack traces in production
280
+
281
+ ---
282
+
283
+ ## 📄 License
284
+
285
+ MIT
286
+
287
+ ---
288
+
289
+ ## 👤 Author
290
+
291
+ Increase
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@increase21/simplenodejs",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "Lightweight Node.js HTTP framework with middleware and plugins",
5
5
  "dev": "dist/index.js",
6
6
  "bugs": "https://github.com/increase21/simplenodejs/issues",
package/readme.md DELETED
File without changes