-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
67 lines (57 loc) · 1.67 KB
/
main.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const bodyParser = require('body-parser');
// Create Express application
const app = express();
app.use(bodyParser.json());
// Array to store todo items (acting as a simple in-memory database)
let todos = [];
// Get all todos
app.get('/todos', (req, res) => {
res.json({ "tasks": ["task one", 4, "task two", "task three"], "empty_str": "", "empty_arr": [], resp_null: null });
});
// Get a specific todo by ID
app.get('/todos/:id', (req, res) => {
const id = parseInt(req.params.id);
const todo = todos.find(todo => todo.id === id);
if (todo) {
res.json(todo);
} else {
res.status(404).json({ error: 'Todo not found' });
}
});
// Create a new todo
app.post('/todos', (req, res) => {
const newTodo = req.body;
newTodo.id = todos.length + 1;
todos.push(newTodo);
res.status(201).json(newTodo);
});
// Update a todo
app.put('/todos/:id', (req, res) => {
const id = parseInt(req.params.id);
const updatedTodo = req.body;
const index = todos.findIndex(todo => todo.id === id);
if (index !== -1) {
todos[index] = { ...todos[index], ...updatedTodo };
res.json(todos[index]);
} else {
res.status(404).json({ error: 'Todo not found' });
}
});
// Delete a todo
app.delete('/todos/:id', (req, res) => {
console.log(req.params.id)
const id = parseInt(req.params.id);
const index = todos.findIndex(todo => todo.id === id);
if (index !== -1) {
const deletedTodo = todos.splice(index, 1);
res.json(deletedTodo[0]);
} else {
res.status(404).json({ error: 'Todo not found' });
}
});
// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});