Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
revised id
  • Loading branch information
batuncer committed Jul 29, 2023
commit 465e23b48f0146d060fa8d4a4eec80eaa8d3faca
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Please ask your mentors for access to this repository.

## Pre-reqs

- [ ] You should have completed at least Level 1 of the Quote Server challenge before attempting this challenge.
- [ ] You should have completed at least Level 1 of the Quote Server challenge before attempting this challenge

## Want to run your code on the internet?

Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2"
"express": "^4.18.2",
"moment": "^2.29.4",
"uuid": "^9.0.0"
},
"engines": {
"node": "8.x"
Expand Down
50 changes: 28 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");
const cors = require("cors");

const port = 3000;
const app = express();

app.use(cors());
Expand All @@ -10,6 +10,7 @@ const welcomeMessage = {
id: 0,
from: "Bart",
text: "Welcome to CYF chat system!",

};

// This array is our "data store".
Expand All @@ -21,33 +22,36 @@ const messages = [
id: 1,
from: "BAki",
text: "CAn you see me?",

},
];

app.get("/", function (request, response) {
response.sendFile(__dirname + "/index.html");
});

app.get("/messages", (request, response) => {
response.send({ messages });
});

// Send a message
app.post("/messages", (request, response) => {
let newMessage = request.body;
if (
newMessage.id === "" ||
newMessage.from === "" ||
newMessage.text === ""
) {
console.error("Invalid message:", newMessage);
response.status(400).send("Bad Request");
} else {
messages.push(newMessage);
response.send("Message posted successfully");
app.post("/messages", (req, res) => {
const newMessage = {
id: messages.length,
from: req.body.from,
text: req.body.text,
timeSent: new Date(),
};

if (!newMessage.from || !newMessage.text) {
return res.status(400).json({
status: "fail",
message: "the message objs have an empty or missing property",
});
}
});

messages.push(newMessage);
res.status(201).send({ newMessage });
});
// Get messages containing the search word
app.get("/messages/search", (request, response) => {
let searchWord = request.query.text;
Expand All @@ -71,9 +75,9 @@ app.delete("/messages/:id", (request, response) => {
response.send(deleteMessageByID(messages, messageId));
});

app.listen(process.env.PORT, () => {
console.log(`Listening on PORT ${process.env.PORT}...`);
});
// app.listen(process.env.PORT, () => {
// console.log(`Listening on PORT ${process.env.PORT}...`);
// });

// Helper function to get a message by ID
const getMessageByID = (messages, id) => {
Expand All @@ -92,16 +96,18 @@ const deleteMessageByID = (messages, id) => {

// Helper function to get messages containing the search word
const getMessagesFromSearch = (messages, word) => {
return messages.filter((message) =>
message.text.toLowerCase().includes(word.toLowerCase())
const lowerCaseWord = word.toLowerCase();
return messages.filter(({ text }) =>
text.toLowerCase().includes(lowerCaseWord)
);
};


// Helper function to get the 10 latest messages
const getLatestMessages = (messages) => {
return messages.slice(Math.max(messages.length - 10, 0));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of slice which return an shallow copy.

};

app.listen(3000, ()=>{
console.log("3000")
app.listen(port, ()=> {
console.log('Listening')
})