Notice: file_put_contents(): Write of 810 bytes failed with errno=28 No space left on device in /var/www/tgoop/post.php on line 50

Warning: file_put_contents(): Only 16384 of 17194 bytes written, possibly out of free disk space in /var/www/tgoop/post.php on line 50
Node.JS [ru] | Серверный JavaScript@we_use_js P.4179
WE_USE_JS Telegram 4179
👩‍💻 Запись лога HTTP-запросов в файл

Напишите HTTP-сервер на Node.js, который записывает информацию о каждом входящем запросе (метод, URL, время) в текстовый файл requests.log. Сервер должен возвращать "Запрос обработан" на любой запрос.

Пример:

node server.js

curl http://localhost:3000/test
curl -X POST http://localhost:3000/api

[2024-11-11T12:00:00Z] GET /test
[2024-11-11T12:00:05Z] POST /api


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

const http = require('http');
const fs = require('fs');

const logFile = 'requests.log';

// Создание HTTP-сервера
const server = http.createServer((req, res) => {
const logEntry = `[${new Date().toISOString()}] ${req.method} ${req.url}\n`;

// Записываем лог в файл
fs.appendFile(logFile, logEntry, (err) => {
if (err) {
console.error('Ошибка записи лога:', err.message);
}
});

// Отправляем ответ
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Запрос обработан');
});

// Запуск сервера на порту 3000
server.listen(3000, () => {
console.log('Сервер запущен на http://localhost:3000');
});
Please open Telegram to view this post
VIEW IN TELEGRAM
4👍1



tgoop.com/we_use_js/4179
Create:
Last Update:

👩‍💻 Запись лога HTTP-запросов в файл

Напишите HTTP-сервер на Node.js, который записывает информацию о каждом входящем запросе (метод, URL, время) в текстовый файл requests.log. Сервер должен возвращать "Запрос обработан" на любой запрос.

Пример:

node server.js

curl http://localhost:3000/test
curl -X POST http://localhost:3000/api

[2024-11-11T12:00:00Z] GET /test
[2024-11-11T12:00:05Z] POST /api


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

const http = require('http');
const fs = require('fs');

const logFile = 'requests.log';

// Создание HTTP-сервера
const server = http.createServer((req, res) => {
const logEntry = `[${new Date().toISOString()}] ${req.method} ${req.url}\n`;

// Записываем лог в файл
fs.appendFile(logFile, logEntry, (err) => {
if (err) {
console.error('Ошибка записи лога:', err.message);
}
});

// Отправляем ответ
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Запрос обработан');
});

// Запуск сервера на порту 3000
server.listen(3000, () => {
console.log('Сервер запущен на http://localhost:3000');
});

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


Share with your friend now:
tgoop.com/we_use_js/4179

View MORE
Open in Telegram


Telegram News

Date: |

Write your hashtags in the language of your target audience. On Tuesday, some local media outlets included Sing Tao Daily cited sources as saying the Hong Kong government was considering restricting access to Telegram. Privacy Commissioner for Personal Data Ada Chung told to the Legislative Council on Monday that government officials, police and lawmakers remain the targets of “doxxing” despite a privacy law amendment last year that criminalised the malicious disclosure of personal information. Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. Just at this time, Bitcoin and the broader crypto market have dropped to new 2022 lows. The Bitcoin price has tanked 10 percent dropping to $20,000. On the other hand, the altcoin space is witnessing even more brutal correction. Bitcoin has dropped nearly 60 percent year-to-date and more than 70 percent since its all-time high in November 2021. Read now
from us


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