-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
85 lines (77 loc) · 2.49 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Importing necessary packages + necessary boilerplate
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const PORT = 3000;
let databaseCurrentID = 1;
const db = [{
id: databaseCurrentID,
message: "Get groceries",
isComplete: false,
}]
// GET /hello
app.get("/hello", (req, res) => {
console.log("Hello world route reached!");
res.send("hello world!");
// res.status(200).json({ "message" : "hello world!" })
});
// GET /todos returns a list of all the todos
app.get("/todos", (req, res) => {
console.log("GET /todos (get all todos)");
res.status(200).json(db);
});
// GET /todos/:id returns a specific todo by ID
// @params:
// - "id" in the path
app.get("/todos/:id", (req, res) => {
console.log(`GET /todos/:id (get a todo by ID) ${req.params.id}`);
let foundTodo = db.find(todo => req.params.id == todo.id);
if (!foundTodo) {
return res.status(404).json({ "error" : "todo not found" });
}
res.status(200).json(foundTodo);
});
// POST /todos creates a new todo and returns it
// @params:
// - "message" in the body
app.post("/todos", (req, res) => {
console.log(`POST /todos (create a new todo)`);
let newTodo = {
id: ++databaseCurrentID,
message: req.body.message,
isComplete: false,
};
db.push(newTodo);
res.status(201).json(newTodo);
});
// PUT /todos/:id updates the message of a todo and returns the todo
// @params:
// - "id" in the path
app.put("/todos/:id", (req, res) => {
console.log(`PUT /todos/:id (update todo by ID) ${req.params.id}`);
let todoId = req.params.id;
let todoToUpdate = db.find(todo => todo.id == todoId);
if (!todoToUpdate) {
return res.status(404).json({ "error" : "todo to update not found" });
}
todoToUpdate.isComplete = req.body.isComplete;
res.status(200).json(todoToUpdate);
});
// DELETE /todos/:id deletes a todo and returns "success": true or false
// @params:
// - "id" in the path
app.delete("/todos/:id", (req, res) => {
console.log(`DELETE /todos/:id (delete todo by ID) ${req.params.id}`);
let todoId = req.params.id;
let todoToDeleteIndex = db.findIndex(todo => todo.id == todoId);
if (todoToDeleteIndex === -1) {
return res.status(404).json({ "error" : "todo to delete not found" });
}
db.splice(todoToDeleteIndex, todoToDeleteIndex + 1);
res.status(200).json({ "success" : true })
});
// Initializes the server on a certain PORT
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}!`);
});