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

Warning: file_put_contents(): Only 12288 of 17766 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.4267
WE_USE_JS Telegram 4267
👩‍💻 Создание простого REST API для управления задачами

Создайте HTTP-сервер на Node.js с REST API для управления списком задач. Реализуйте следующие функции:

Получение всех задач (GET /tasks).
Добавление новой задачи (POST /tasks).
Удаление задачи по индексу (DELETE /tasks/:index).

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

npm init -y
npm install express

const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

let tasks = [];

// Получение всех задач
app.get('/tasks', (req, res) => {
res.json(tasks);
});

// Добавление новой задачи
app.post('/tasks', (req, res) => {
const { task } = req.body;
if (task) {
tasks.push(task);
res.status(201).json({ message: 'Задача добавлена', tasks });
} else {
res.status(400).json({ error: 'Задача не должна быть пустой' });
}
});

// Удаление задачи по индексу
app.delete('/tasks/:index', (req, res) => {
const index = parseInt(req.params.index, 10);
if (index >= 0 && index < tasks.length) {
tasks.splice(index, 1);
res.json({ message: 'Задача удалена', tasks });
} else {
res.status(404).json({ error: 'Задача не найдена' });
}
});

// Запуск сервера
app.listen(port, () => {
console.log(`Сервер запущен на http://localhost:${port}`);
});
Please open Telegram to view this post
VIEW IN TELEGRAM
👍41🤡1



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

👩‍💻 Создание простого REST API для управления задачами

Создайте HTTP-сервер на Node.js с REST API для управления списком задач. Реализуйте следующие функции:

Получение всех задач (GET /tasks).
Добавление новой задачи (POST /tasks).
Удаление задачи по индексу (DELETE /tasks/:index).

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

npm init -y
npm install express

const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

let tasks = [];

// Получение всех задач
app.get('/tasks', (req, res) => {
res.json(tasks);
});

// Добавление новой задачи
app.post('/tasks', (req, res) => {
const { task } = req.body;
if (task) {
tasks.push(task);
res.status(201).json({ message: 'Задача добавлена', tasks });
} else {
res.status(400).json({ error: 'Задача не должна быть пустой' });
}
});

// Удаление задачи по индексу
app.delete('/tasks/:index', (req, res) => {
const index = parseInt(req.params.index, 10);
if (index >= 0 && index < tasks.length) {
tasks.splice(index, 1);
res.json({ message: 'Задача удалена', tasks });
} else {
res.status(404).json({ error: 'Задача не найдена' });
}
});

// Запуск сервера
app.listen(port, () => {
console.log(`Сервер запущен на http://localhost:${port}`);
});

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


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

View MORE
Open in Telegram


Telegram News

Date: |

Channel login must contain 5-32 characters With the administration mulling over limiting access to doxxing groups, a prominent Telegram doxxing group apparently went on a "revenge spree." As the broader market downturn continues, yelling online has become the crypto trader’s latest coping mechanism after the rise of Goblintown Ethereum NFTs at the end of May and beginning of June, where holders made incoherent groaning sounds and role-played as urine-loving goblin creatures in late-night Twitter Spaces. It’s yet another bloodbath on Satoshi Street. As of press time, Bitcoin (BTC) and the broader cryptocurrency market have corrected another 10 percent amid a massive sell-off. Ethereum (EHT) is down a staggering 15 percent moving close to $1,000, down more than 42 percent on the weekly chart. Concise
from us


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