London class-10- Junita Lama-Node module-week 2-chat server react app#290
London class-10- Junita Lama-Node module-week 2-chat server react app#290Junitalama wants to merge 4 commits intoCodeYourFuture:masterfrom
Conversation
ShayanMahnam
left a comment
There was a problem hiding this comment.
Great job, Junita! Your work is amazing. I'll provide some advice and feedback in further comments.
| @@ -20,4 +26,53 @@ app.get("/", function (request, response) { | |||
| response.sendFile(__dirname + "/index.html"); | |||
There was a problem hiding this comment.
You have your own front end now you dont need this anymore
|
|
||
| //send messages | ||
|
|
||
| app.post("/messages", function (request, response) { |
There was a problem hiding this comment.
you can do error handling like this and generate id for msgs here:(generate id here its just a example in real world we dont use this we will use uuid)
app.post("/messages", function (request, response) {
const newMessage = request.body;
// Check if the 'from' and 'text' properties are present in the request body
if (!newMessage.from || !newMessage.text) {
return response.status(400).json({ error: "Both 'from' and 'text' are required." });
} else {
// Generate a new unique id for the message
newMessage.id = messages.length;
messages.push(newMessage);
response.status(201).json(newMessage);
console.log(newMessage);
}
});| response.json(messages); | ||
| }); | ||
|
|
||
| app.get("/messages/:id", function (request, response) { |
There was a problem hiding this comment.
you can validate once more to see if the msg with that id is there
app.get("/messages/:id", function (request, response) {
const idToFind = Number(request.params.id);
const message = messages.find((msg) => msg.id === idToFind);
// Check if the message with the provided ID exists
if (message) {
response.json(message);
} else {
response.status(404).json({ error: "Message not found." });
}
});|
|
||
| //delete messages | ||
|
|
||
| app.delete("/messages/:id", function (request, response) { |
There was a problem hiding this comment.
same check check if the message exists
app.delete("/messages/:id", function (request, response) {
const idToDelete = Number(request.params.id);
const messageIndex = messages.findIndex((msg) => msg.id === idToDelete);
// Check if the message with the provided ID exists
if (messageIndex !== -1) {
const deletedMessage = messages.splice(messageIndex, 1);
response.json(deletedMessage[0]);
} else {
response.status(404).json({ error: "Message not found." });
}
});|
|
||
| //latest messages | ||
|
|
||
| app.get("/messages/latest", (req, res) => { |
There was a problem hiding this comment.
app.get("/messages/latest", (req, res) => {
const startIndex = Math.max(messages.length - 10, 0);
const latestMessages = messages.slice(startIndex);
res.json(latestMessages);
});| res.json(result); | ||
| }); | ||
|
|
||
| app.get("/messages/search", (req, res) => { |
There was a problem hiding this comment.
app.get("/messages/search", (req, res) => {
const searchText = req.query.text.toLowerCase();
const result = messages.filter((msg) => msg.text.toLowerCase().includes(searchText));
res.json(result);
});| ); | ||
| res.send(result); | ||
| }); | ||
|
|
There was a problem hiding this comment.
its good to have some msgs for app.linsten to make sure when you run it, its working
const port = 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
@ShayanMahnam Thank you so much for reviewing it. |
Volunteers: Are you marking this coursework? You can find a guide on how to mark this coursework in
HOW_TO_MARK.mdin the root of this repositoryYour Details
Homework Details
Notes
What did you find easy?
What did you find hard?
What do you still not understand?
Any other notes?