Uh oh!
There was an error while loading. Please reload this page.
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 into
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){constnewMessage=request.body;// Check if the 'from' and 'text' properties are present in the request bodyif(!newMessage.from||!newMessage.text){returnresponse.status(400).json({error: "Both 'from' and 'text' are required."});}else{// Generate a new unique id for the messagenewMessage.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){constidToFind=Number(request.params.id);constmessage=messages.find((msg)=>msg.id===idToFind);// Check if the message with the provided ID existsif(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){constidToDelete=Number(request.params.id);constmessageIndex=messages.findIndex((msg)=>msg.id===idToDelete);// Check if the message with the provided ID existsif(messageIndex!==-1){constdeletedMessage=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)=>{conststartIndex=Math.max(messages.length-10,0);constlatestMessages=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)=>{constsearchText=req.query.text.toLowerCase();constresult=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
constport=8080;app.listen(port,()=>{console.log(`Server is running on http://localhost:${port}`);});Junitalama
commented
Jul 29, 2023
@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?