Notice: file_put_contents(): Write of 9012 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 17204 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.4161
WE_USE_JS Telegram 4161
👩‍💻 Запись лога 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👍2



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

View MORE
Open in Telegram


Telegram News

Date: |

Avoid compound hashtags that consist of several words. If you have a hashtag like #marketingnewsinusa, split it into smaller hashtags: “#marketing, #news, #usa. Telegram offers a powerful toolset that allows businesses to create and manage channels, groups, and bots to broadcast messages, engage in conversations, and offer reliable customer support via bots. With the “Bear Market Screaming Therapy Group,” we’ve now transcended language. Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. Find your optimal posting schedule and stick to it. The peak posting times include 8 am, 6 pm, and 8 pm on social media. Try to publish serious stuff in the morning and leave less demanding content later in the day.
from us


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