-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (51 loc) · 1.46 KB
/
server.js
File metadata and controls
60 lines (51 loc) · 1.46 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
import express from "express";
import mailinglist from "./mailing-lists.js";
const PORT = 3000;
const app = express();
app.use(express.json());
app.disable("x-powered-by");
const mailList = mailinglist;
// fetch all list names
app.get("/lists", (req, res) => {
const listsArray = Object.keys(mailList);
res.status(200).json(listsArray);
});
// get a single name
app.get("/lists/:name", (req, res) => {
const name = req.params.name;
const list = mailList[name];
if (list) {
res.status(200).json({ name, members: list });
} else {
res.status(404).json({ error: "List not found" });
}
});
// delete a name
app.delete("/lists/:name", (req, res) => {
const name = req.params.name;
if (mailList[name]) {
delete mailList[name];
res.status(200).json({ message: "Successfully deleted" });
} else {
res.status(404).json({ message: "Name not found for delete" });
}
});
// add or update a list
app.put("/lists/:name", (req, res) => {
const pathName = req.params.name;
const { name, members } = req.body;
if (pathName !== name) {
res.status(400).json({ error: "Name in path does not match name in body" });
return;
}
if (mailList[name]) {
mailList[name] = members;
res.status(200).json({ message: "List updated successfully." });
} else {
mailList[name] = members;
res.status(201).json({ message: "New list created successfully." });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});