- aggiunto componenti per Home Template... ma ancora da provare
- sistemato catprods - Sistemato menu
This commit is contained in:
6
src/server/middleware/asyncHandler.js
Normal file
6
src/server/middleware/asyncHandler.js
Normal file
@@ -0,0 +1,6 @@
|
||||
// @ts-check
|
||||
const ah = (fn) => (req, res, next) =>
|
||||
Promise.resolve(fn(req, res, next)).catch((err) => next(err));
|
||||
|
||||
|
||||
module.exports = { ah };
|
||||
13
src/server/middleware/error.js
Normal file
13
src/server/middleware/error.js
Normal file
@@ -0,0 +1,13 @@
|
||||
// @ts-check
|
||||
function notFound(_req, res) {
|
||||
res.status(404).json({ message: 'Not Found' });
|
||||
}
|
||||
|
||||
function errorHandler(err, _req, res, _next) {
|
||||
const status = err.status || 500;
|
||||
const message = err.message || 'Server Error';
|
||||
if (status >= 500) console.error(err);
|
||||
res.status(status).json({ message });
|
||||
}
|
||||
|
||||
module.exports = { notFound, errorHandler };
|
||||
21
src/server/middleware/rateLimit.js
Normal file
21
src/server/middleware/rateLimit.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// @ts-check
|
||||
const buckets = new Map();
|
||||
/** 10 secondi */
|
||||
const WINDOW_MS = 10_000;
|
||||
const LIMIT = 100;
|
||||
|
||||
function rateLimit(req, res, next) {
|
||||
const key = req.ip || 'global';
|
||||
const now = Date.now();
|
||||
const bucket = buckets.get(key) || { count: 0, ts: now };
|
||||
if (now - bucket.ts > WINDOW_MS) {
|
||||
bucket.count = 0;
|
||||
bucket.ts = now;
|
||||
}
|
||||
bucket.count++;
|
||||
buckets.set(key, bucket);
|
||||
if (bucket.count > LIMIT) return res.status(429).json({ message: 'Troppo traffico, riprova tra poco.' });
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = { rateLimit };
|
||||
Reference in New Issue
Block a user