-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
112 lines (96 loc) · 2.55 KB
/
main.js
File metadata and controls
112 lines (96 loc) · 2.55 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const express = require("express");
const fs = require("fs");
const app = express();
const multer = require("multer");
const upload = multer({ dest: "uploads/" });
const port = 3000;
app.use(express.static("public"));
app.use(express.static("uploads"));
app.use(express.json());
app.get("/", (req, res) => {
res.send("Sorry ! Page Not Found");
});
//Saving an image to uploads
app.post("/setImage", upload.single("taskImg"), function (req, res) {
let path = JSON.stringify(req.file.filename);
res.send(path);
});
//Saving an object to ToDoList
app.post("/saveToDo", function (req, res) {
// console.log(req.body);
fs.readFile(__dirname + "/data/data.txt", "utf-8", (err, data) => {
if (err) console.log(err);
let todos;
if (data.length === 0) todos = [];
else todos = JSON.parse(data);
//Seting object
let obj = req.body;
//new object
if (obj.id > todos.length) {
todos.push(req.body);
}
//Update object (override)
else {
let index = obj.id - 1;
todos[index] = obj;
}
fs.writeFile("./data/data.txt", JSON.stringify(todos), () => {
res.end();
});
});
});
//Getting an item from ToDo List
app.post("/getItem", function (req, res) {
// console.log(req.body);
fs.readFile(__dirname + "/data/data.txt", "utf-8", (err, data) => {
if (err) console.log(err);
let todos;
if (data.length === 0) res.send("-1");
else todos = JSON.parse(data);
//Getting index
let index = req.body.index - 1;
if (index > todos.length) res.send("-1");
else {
res.json(todos[index]);
}
});
});
//Getting all items from ToDo List
app.get("/getToDo", function (req, res) {
fs.readFile(__dirname + "/data/data.txt", "utf-8", (err, data) => {
if (err) console.log(err);
let todos;
if (data.length === 0) todos = [];
else todos = JSON.parse(data);
res.json(todos);
});
});
//Deleting an item from ToDo List
app.post("/deleteToDo", function (req, res) {
// console.log(req.body);
fs.readFile(__dirname + "/data/data.txt", "utf-8", (err, data) => {
if (err) console.log(err);
let todos;
if (data.length === 0) res.send("-1");
else todos = JSON.parse(data);
//Getting index
let index = req.body.index - 1;
// console.log(index);
if (index > todos.length) res.send("-1");
else {
todos[index] = null;
fs.writeFile("./data/data.txt", JSON.stringify(todos), () => {
res.end();
});
}
});
});
//Clearing all items from ToDo List
app.get("/clearData", function (req, res) {
fs.writeFile("./data/data.txt", "", () => {
res.end();
});
});
app.listen(port, () => {
console.log("Example app listening at port: ", port);
});