Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions chat-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.DS_Store
39 changes: 31 additions & 8 deletions chat-app/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
# Write and Deploy Chat Application Frontend and Backend

### To start the program

First install dependencies:

```bash
npm install
```

From `/backend` directory:

Option 1. Production mode

```bash
npm start
```

Option 2. Development mode

```bash
npm run dev
```

### Link to the coursework

https://sdc.codeyourfuture.io/decomposition/sprints/2/prep/

You must complete and deploy a chat application. You have two weeks to complete this.

It must support at least the following requirements:
* As a user, I can send add a message to the chat.
* As a user, when I open the chat I see the messages that have been sent by any user.
* As a user, when someone sends a message, it gets added to what I see.

- As a user, I can send add a message to the chat.
- As a user, when I open the chat I see the messages that have been sent by any user.
- As a user, when someone sends a message, it gets added to what I see.

It must also support at least one additional feature.

Expand All @@ -27,8 +50,8 @@ Exploring and understanding different ways of sending information between a clie

### How to submit

* Fork the Module-Decomposition repository
* Develop and deploy your chat app
* Create a pull request back into the original Module-Decomposition repo, including:
* A link to the deployed frontend on the CYF hosting environment
* A link to the deployed backend on the CYF hosting environment
- Fork the Module-Decomposition repository
- Develop and deploy your chat app
- Create a pull request back into the original Module-Decomposition repo, including:
- A link to the deployed frontend on the CYF hosting environment
- A link to the deployed backend on the CYF hosting environment
109 changes: 109 additions & 0 deletions chat-app/backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import express from "express";
import cors from "cors";

const app = express();
app.use(cors());
app.use(express.json());

const port = process.env.PORT || 3000;
let nextMessageId = 1;
const messages = [
Comment on lines +8 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Better to declare global constants and variables before const app = express() because lines 5-6 are already logic to setup the server (app).

{
id: nextMessageId++,
message: "Hello",
user: "Jane",
time: Date.now() - 6000,
likes: 1,
dislikes: 2,
},
{
id: nextMessageId++,
message: "Hey",
user: "John",
time: Date.now() - 3000,
likes: 1,
dislikes: 2,
},
{
id: nextMessageId++,
message: "Hi",
user: "Bob",
time: Date.now(),
likes: 1,
dislikes: 2,
},
];
const callbacksForNewMessages = [];

app.get("/", (req, res) => {
let since = Number(req.query.since);
let longPoll = req.query.longPoll === "true";

if (since) {
const messagesToSend = messages.filter((msg) => msg.time > since);
if (messagesToSend.length === 0 && longPoll) {
callbacksForNewMessages.push((val) => res.json(val));
return;
} else res.json(messagesToSend);
return;
}
res.json(messages);
});

app.post("/", (req, res) => {
const { message, user } = req.body;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's good practice to validate all request inputs.

Note: A more robust implementation

  • Should also handle the case where req.body is null, which would cause const { text, sender } = req.body; to throw.

In this exercise, let's assume the request is always coming from the code in script.js. So no change needed.


if (
typeof message !== "string" ||
typeof user !== "string" ||
!message.trim() ||
!user.trim()
) {
res.status(400).json({ error: `Message and user are required!` });
return;
}

const newMessage = {
id: nextMessageId++,
message,
user,
Comment on lines +59 to +69
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's a bit inconsistent that message containing only space characters are not allowed but leading and trailing space characters are acceptable.

time: Date.now(),
likes: 0,
dislikes: 0,
};

messages.push(newMessage);
while (callbacksForNewMessages.length > 0) {
const callback = callbacksForNewMessages.pop();
callback([messages[messages.length - 1]]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wouldn't sending newMessage shorter in syntax?

}
res.status(201).json({ success: true });
});

app.post("/:id/like", (req, res) => {
const id = Number(req.params.id);
const message = messages.find((msg) => msg.id === id);

if (!message) {
res.status(404).json({ error: "Message not found" });
return;
}
message.likes++;
res.json(message);
});

app.post("/:id/dislike", (req, res) => {
const id = Number(req.params.id);
const message = messages.find((msg) => msg.id === id);

if (!message) {
res.status(404).json({ error: "Message not found" });
return;
}
message.dislikes++;
res.json(message);
});

app.listen(port, () => {
console.log(`Chat server listening on port ${port}`);
});
Loading
Loading