-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (50 loc) · 1.42 KB
/
server.js
File metadata and controls
50 lines (50 loc) · 1.42 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
const express = require("express");
const data = require("./mailing-lists");
const app = express();
app.use(express.json());
const lists = new Map(Object.entries(data));
app.get("/lists", (req, res) => {
const listsArray = Array.from(lists.keys());
if (listsArray.length > 0) {
res.status(200).send(listsArray);
} else {
res.status(200).send({});
}
});
app.get("/lists/:name", (req, res) => {
const name = req.params.name.toLowerCase();
const listsArray = lists.get(name);
if (listsArray) {
res.status(200).send(listsArray);
} else {
res.status(404).send("list not found");
}
});
app.delete("/lists/:name", (req, res) => {
const name = req.params.name.toLowerCase();
if (lists.has(name)) {
lists.delete(name);
const updatedList = Array.from(lists.keys());
res.status(200).send(updatedList);
} else {
res.status(404).send("list not found");
}
});
app.put("/lists/:name", (req, res) => {
const name = req.params.name.toLowerCase();
if (lists.has(name)) {
const putListBody = req.body;
lists.set(name, putListBody);
const updatedList = Array.from(lists.keys());
res.status(200).send(updatedList);
} else {
const putListBody = req.body;
console.log(putListBody);
lists.set(name, putListBody);
const updatedList = Array.from(lists.keys());
res.status(201).send(updatedList);
}
});
app.listen(3000, () => {
console.log("Server is listen to 3000");
});