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

Warning: file_put_contents(): Only 8192 of 17057 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.4332
WE_USE_JS Telegram 4332
👩‍💻 Запись лога 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
👍3



tgoop.com/we_use_js/4332
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/4332

View MORE
Open in Telegram


Telegram News

Date: |

The group’s featured image is of a Pepe frog yelling, often referred to as the “REEEEEEE” meme. Pepe the Frog was created back in 2005 by Matt Furie and has since become an internet symbol for meme culture and “degen” culture. The Channel name and bio must be no more than 255 characters long Developing social channels based on exchanging a single message isn’t exactly new, of course. Back in 2014, the “Yo” app was launched with the sole purpose of enabling users to send each other the greeting “Yo.” SUCK Channel Telegram 4How to customize a Telegram channel?
from us


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