-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathserver.js
More file actions
124 lines (102 loc) · 3.22 KB
/
server.js
File metadata and controls
124 lines (102 loc) · 3.22 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
113
114
115
116
117
118
119
120
121
122
123
124
process.env.PORT = process.env.PORT || 9090;
import express, { json } from "express";
import cors from "cors";
import path from "path";
import { fileURLToPath } from "url";
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
// Get __dirname in ES module
const __dirname = path.dirname(fileURLToPath(import.meta.url));
//This array is our "data store".
//We will start with one message in the array.
const messages = [
{
id: 0,
from: "Bart",
text: "Welcome to CYF chat system!",
timeSent: new Date(),
},
{
id: 1,
from: "test",
text: "test",
timeSent: new Date(),
},
];
let lastId = 1;
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// Get all messages
app.get("/messages", (req, res) => {
res.send(messages);
});
//Level 3 - more "read" functionality
app.get("/messages/search", (req, res) => {
console.log("here");
const searchText = req.query.text;
if (!searchText) {
return res.status(400).json({ message: "Please provide a 'text' query parameter" });
}
const filteredMessages = messages.filter((message) => message.text.includes(searchText));
res.json(filteredMessages);
});
app.get("/messages/latest", (req, res) => {
const latestMessages = messages.slice(-10);
res.json(latestMessages);
});
// GET a specific message by id
app.get("/messages/:id", (req, res) => {
const messageId = parseInt(req.params.id);
const message = messages.find((p) => p.id === messageId);
if (!message) return res.status(404).json({ message: "Message not found" });
res.json(message);
});
// POST a new message
app.post("/messages", (req, res) => {
const newId = (lastId += 1);
if (!req.body.from) {
return res.status(422).json({ message: "From field is required" });
} //A 422 status code indicates that the server was unable to process the request because it contains invalid data.
if (!req.body.text) {
return res.status(422).json({ message: "Text field is required" });
}
const timeSent = new Date(); // Adding timestamp
const message = {
id: newId,
from: req.body.from,
text: req.body.text,
timeSent: timeSent,
};
lastId = newId;
messages.push(message);
res.status(201).json(message);
});
// Level 5 - Optional - PUT to update a message
app.put("/messages/:id", (req, res) => {
const messageId = parseInt(req.params.id);
const messageIndex = messages.findIndex((message) => message.id === messageId);
if (messageIndex === -1) {
return res.status(404).json({ message: "Message not found for an update" });
}
const updatedMessage = messages[messageIndex];
if (req.body.text !== undefined) {
updatedMessage.text = req.body.text;
}
if (req.body.from !== undefined) {
updatedMessage.from = req.body.from;
}
res.json(updatedMessage);
});
// DELETE a message
app.delete("/messages/:id", (req, res) => {
const index = messages.findIndex((p) => p.id === parseInt(req.params.id));
if (index === -1) return res.status(404).json({ message: "Message not found for delete" });
messages.splice(index, 1);
res.json({ message: "Message deleted" });
});
app.listen(process.env.PORT, () => {
console.log(`listening on PORT ${process.env.PORT}...`);
});