-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.js
More file actions
39 lines (34 loc) · 981 Bytes
/
tasks.js
File metadata and controls
39 lines (34 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const axios = require('axios');
const fetchTasks = async () => {
try {
const response = await axios.get('http://localhost:3000/api/tasks/');
if (response.status !== 200) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
console.log('Tasks:', response.data);
} catch (error) {
console.error('Error fetching tasks:', error.message);
}
};
const addTask = async (title, description) => {
const newTask = {
title,
description,
};
try {
const response = await axios.post('http://localhost:3000/api/tasks/', newTask, {
headers: {
'Content-Type': 'application/json',
},
});
if (response.status !== 201) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
console.log('Task added:', response.data);
} catch (error) {
console.error('Error adding task:', error.message);
}
};
// Example usage
fetchTasks();
addTask('New Task', 'This is a new task description.');