Warning: mkdir(): No space left on device in /var/www/hottg/post.php on line 59

Warning: file_put_contents(aCache/aDaily/2025-07-19/post/we_use_js/--): Failed to open stream: No such file or directory in /var/www/hottg/post.php on line 72
👩‍💻 Мини-сервер с авто-ограничением запросов по IP @Node.JS [ru] | Серверный JavaScript
TG Telegram Group & Channel
Node.JS [ru] | Серверный JavaScript | United States America (US)
Create: Update:

👩‍💻 Мини-сервер с авто-ограничением запросов по IP

Реализуйте простой HTTP-сервер на Node.js (без фреймворков), который обрабатывает GET-запросы и возвращает "Hello, client!".

Но с одним условием: если IP-адрес клиента делает больше 5 запросов за 10 секунд, сервер должен временно блокировать этот IP на 30 секунд и отвечать ему "429 Too Many Requests".

Решение задачи🔽

const http = require('http');
const PORT = 3000;

const requestLog = {}; // { ip: [timestamps] }
const bannedIPs = {}; // { ip: unblockTimestamp }

function cleanupOldRequests(ip) {
const now =
Date.now();
requestLog[ip] = (requestLog[ip] || []).filter(ts => now - ts < 10000);
}

const server = http.createServer((req, res) => {
const ip = req.socket.remoteAddress;

if (bannedIPs[ip] &&
Date.now() < bannedIPs[ip]) {
res.writeHead(429, { 'Content-Type': 'text/plain' });
return res.end('429 Too Many Requests');
}

cleanupOldRequests(ip);
requestLog[ip].push(
Date.now());

if (requestLog[ip].length > 5) {
bannedIPs[ip] =
Date.now() + 30000; // блок на 30 сек
requestLog[ip] = [];
res.writeHead(429, { 'Content-Type': 'text/plain' });
return res.end('429 Too Many Requests');
}

res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, client!');
});

server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});

👩‍💻 Мини-сервер с авто-ограничением запросов по IP

Реализуйте простой HTTP-сервер на Node.js (без фреймворков), который обрабатывает GET-запросы и возвращает "Hello, client!".

Но с одним условием: если IP-адрес клиента делает больше 5 запросов за 10 секунд, сервер должен временно блокировать этот IP на 30 секунд и отвечать ему "429 Too Many Requests".

Решение задачи🔽

const http = require('http');
const PORT = 3000;

const requestLog = {}; // { ip: [timestamps] }
const bannedIPs = {}; // { ip: unblockTimestamp }

function cleanupOldRequests(ip) {
const now =
Date.now();
requestLog[ip] = (requestLog[ip] || []).filter(ts => now - ts < 10000);
}

const server = http.createServer((req, res) => {
const ip = req.socket.remoteAddress;

if (bannedIPs[ip] &&
Date.now() < bannedIPs[ip]) {
res.writeHead(429, { 'Content-Type': 'text/plain' });
return res.end('429 Too Many Requests');
}

cleanupOldRequests(ip);
requestLog[ip].push(
Date.now());

if (requestLog[ip].length > 5) {
bannedIPs[ip] =
Date.now() + 30000; // блок на 30 сек
requestLog[ip] = [];
res.writeHead(429, { 'Content-Type': 'text/plain' });
return res.end('429 Too Many Requests');
}

res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, client!');
});

server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
Please open Telegram to view this post
VIEW IN TELEGRAM
👍7


>>Click here to continue<<

Node.JS [ru] | Серверный JavaScript




Share with your best friend
VIEW MORE

United States America Popular Telegram Group (US)


Warning: Undefined array key 3 in /var/www/hottg/function.php on line 115

Fatal error: Uncaught mysqli_sql_exception: Can't create/write to file '/tmp/#sql-temptable-a06e-537e55-1b69.MAI' (Errcode: 28 "No space left on device") in /var/www/hottg/function.php:216 Stack trace: #0 /var/www/hottg/function.php(216): mysqli_query() #1 /var/www/hottg/function.php(115): select() #2 /var/www/hottg/post.php(351): daCache() #3 /var/www/hottg/route.php(63): include_once('...') #4 {main} thrown in /var/www/hottg/function.php on line 216