-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
69 lines (60 loc) · 1.84 KB
/
server.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
/* eslint-env node */
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const uuid = require("uuid");
const port = 8081;
const app = express();
const delay = 100;
const respondWithDelay = (respond) => {
setTimeout(respond, delay);
};
app.use(cors());
app.use(bodyParser.json());
const initialComments = [
{ id: uuid.v4(), author: "dog", text: "woof" },
{ id: uuid.v4(), author: "cat", text: "meow" },
{ id: uuid.v4(), author: "giraffe", text: "gnaaaaaa" },
{ id: uuid.v4(), author: "neo", text: "red pill" },
];
let currentComments = initialComments;
app.get("/comments", (req, res) => {
respondWithDelay(() => {
console.log("Responding with comments", currentComments);
res.status(200).send(currentComments);
});
});
app.post("/comments", (req, res) => {
const { author, text } = req.body;
if (!author || !text) {
respondWithDelay(() =>
res.status(422).send({ error: "author and text must be specified" })
);
} else {
const id = uuid.v4();
const newComment = { id, author, text };
currentComments = currentComments.concat(newComment);
respondWithDelay(() => {
console.log("Added comment", newComment);
res.status(201).send({ id });
});
}
});
app.delete("/comments/:commentId", (req, res) => {
const commentId = req.params.commentId;
const commentIndex = currentComments.findIndex((el) => el.id === commentId);
if (commentIndex >= 0) {
respondWithDelay(() => {
console.log("Deleted comment", currentComments[commentIndex]);
currentComments = currentComments.filter((el) => el.id !== commentId);
res.status(200).send({});
});
} else {
respondWithDelay(() => {
res.status(404).send({});
});
}
});
app.listen(port, () => {
console.log(`${new Date()} Server is listening on port ${port}`);
});