fixed development testing for the API

This commit is contained in:
Robert MacRae 2026-01-25 10:33:52 -04:00
parent 7a8b0672aa
commit 5baa2c8e09

View File

@ -47,23 +47,32 @@ app.use(express.urlencoded({ extended: true }));
// Static file serving // Static file serving
app.use(express.static('../Website/dist')); app.use(express.static('../Website/dist'));
// Rate limiting // Rate limiting (more lenient for development)
const { RateLimiterMemory } = require('rate-limiter-flexible'); const { RateLimiterMemory } = require('rate-limiter-flexible');
const limiter = new RateLimiterMemory({ const limiter = new RateLimiterMemory({
keyGenerator: (req) => req.ip, keyGenerator: (req) => req.ip,
points: 100, // limit each IP to 100 requests per windowMs points: 1000, // limit each IP to 1000 requests per windowMs (increased from 100)
duration: 900, // 15 minutes duration: 60, // 1 minute window (reduced from 15 minutes)
blockDuration: 900, // Block for 15 minutes blockDuration: 60, // Block for 1 minute (reduced from 15 minutes)
}); });
app.use('/api/', async (req, res, next) => { app.use('/api/', async (req, res, next) => {
try { try {
const resLimiter = await limiter.consume(req.ip); // Skip rate limiting for localhost in development
if (!resLimiter.remainingPoints) { const isLocalhost = req.ip === '127.0.0.1' || req.ip === '::1' || req.hostname === 'localhost';
return res.status(429).json({ error: 'Too many requests, please try again later.' });
if (!isLocalhost) {
const resLimiter = await limiter.consume(req.ip);
if (!resLimiter.remainingPoints) {
return res.status(429).json({ error: 'Too many requests, please try again later.' });
}
} }
next(); next();
} catch (error) { } catch (rejRes) {
next(); // Handle rate limit exceeded
const secs = Math.round(rejRes.msBeforeNext / 1000) || 1;
res.set('Retry-After', String(secs));
res.status(429).json({ error: 'Too many requests, please try again later.' });
} }
}); });