-
-
Notifications
You must be signed in to change notification settings - Fork 445
London class-10- Junita Lama-Node module-week 2-chat server react app #290
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job, Junita! Your work is amazing. I'll provide some advice and feedback in further comments.
const messages = [welcomeMessage]; | ||
|
||
app.get("/", function (request, response) { | ||
response.sendFile(__dirname + "/index.html"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.md
in 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?