express-project-builder 1.0.13 → 1.0.15
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 +78 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -345,3 +345,81 @@ import notFound from "./app/middlewares/notFound";
|
|
|
345
345
|
app.use(globalErrorHandler as unknown as express.ErrorRequestHandler);
|
|
346
346
|
app.use(notFound as unknown as express.ErrorRequestHandler);
|
|
347
347
|
```
|
|
348
|
+
|
|
349
|
+
- /src/middlewares/**handleFileUpload.ts**
|
|
350
|
+
Middleware for handling file uploads in multipart/form-data requests. Uses `multer` to process uploaded files and stores them in the specified directory. Returns an array of file objects (`req.files`) to the next middleware.
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
// In your route file
|
|
354
|
+
import { handle_file_upload_middleware } from "../../middlewares/handleFileUpload";
|
|
355
|
+
|
|
356
|
+
// Apply the middleware to the route
|
|
357
|
+
router.post(
|
|
358
|
+
"/create",
|
|
359
|
+
handle_file_upload_middleware([
|
|
360
|
+
{
|
|
361
|
+
fieldName: "image", // Form field name for the file
|
|
362
|
+
maxCount: 1, // Maximum number of files allowed
|
|
363
|
+
fileType: "image", // Type of file (image, document, etc.)
|
|
364
|
+
optional: false, // Whether the file is required,
|
|
365
|
+
allowedImageTypes: [
|
|
366
|
+
"image/jpeg",
|
|
367
|
+
"image/png",
|
|
368
|
+
"image/gif",
|
|
369
|
+
"image/webp",
|
|
370
|
+
"image/bmp",
|
|
371
|
+
"image/svg+xml",
|
|
372
|
+
], // Allowed image types,
|
|
373
|
+
maxImageSize: 1024 * 1024 * 2, // Maximum image size in bytes (2MB)
|
|
374
|
+
},
|
|
375
|
+
]),
|
|
376
|
+
formDataToSetJSONformatData, // Convert form data to JSON
|
|
377
|
+
ProductController.createProduct
|
|
378
|
+
);
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
- /src/middlewares/**rateLimitingHandler.ts**
|
|
382
|
+
Rate limiting middleware to prevent API abuse. Implements progressive rate limiting that increases block duration for repeat offenders.
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
// In your app.ts file for global rate limiting or
|
|
386
|
+
// In your route file for route-specific rate limiting
|
|
387
|
+
|
|
388
|
+
import { createProgressiveRateLimiter } from "../../middlewares/rateLimitingHandler";
|
|
389
|
+
|
|
390
|
+
// Create rate limiter instances with different configurations
|
|
391
|
+
const apiRateLimiter = createProgressiveRateLimiter({
|
|
392
|
+
initialWindowMs: 60 * 1000, // 1 minute
|
|
393
|
+
initialMax: 100, // limit each IP to 100 requests per windowMs
|
|
394
|
+
initialBlockMs: 15 * 60 * 1000, // block for 15 minutes on first offense
|
|
395
|
+
message: {
|
|
396
|
+
success: false,
|
|
397
|
+
message: "Too many requests from this IP, please try again later.",
|
|
398
|
+
},
|
|
399
|
+
skipSuccessfulRequests: false,
|
|
400
|
+
skipFailedRequests: false,
|
|
401
|
+
// Custom keyGenerator to handle proxied requests
|
|
402
|
+
keyGenerator: (req: Request) => {
|
|
403
|
+
// Check for X-Forwarded-For header (common when behind proxy)
|
|
404
|
+
const forwarded = req.headers["x-forwarded-for"] as string;
|
|
405
|
+
if (forwarded) {
|
|
406
|
+
// If multiple IPs, take the first one (client IP)
|
|
407
|
+
return forwarded.split(",")[0].trim();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Fallback to other methods
|
|
411
|
+
return (
|
|
412
|
+
req.ip ||
|
|
413
|
+
req.socket?.remoteAddress ||
|
|
414
|
+
req.connection?.remoteAddress ||
|
|
415
|
+
"unknown"
|
|
416
|
+
);
|
|
417
|
+
},
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
// Apply rate limiter to a specific route
|
|
421
|
+
router.get("/products", apiRateLimiter, ProductControllers.getAllProducts);
|
|
422
|
+
|
|
423
|
+
// Apply rate limiter globally to all API routes
|
|
424
|
+
app.use("/v1/api/", apiRateLimiter, routers);
|
|
425
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-project-builder",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "A powerful and professional Express.js project generator CLI that instantly scaffolds a production-ready backend with TypeScript, modular architecture, and built-in support for MongoDB (Mongoose) or PostgreSQL (Prisma). Includes authentication, error handling, rate limiting, file upload, caching, and utility functions—so you can focus on building features instead of boilerplate. Perfect for kickstarting your next Express.js API project with best practices and modern tools.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/bin/index.js",
|