-
-
Notifications
You must be signed in to change notification settings - Fork 52
NW | 26-SDC-Mar | TzeMing Ho | Sprint 2 | Chat App frontend and backend #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
474cc24
da4170f
4c084cb
1f6e2a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| **/node_modules/ | ||
| .env | ||
| dist/ | ||
| .DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import express from "express"; | ||
| import cors from "cors"; | ||
|
|
||
| const app = express(); | ||
| app.use( | ||
| cors({ | ||
| // origin: "https://tzemingho-chatapp-server-frontend.hosting.codeyourfuture.io" | ||
| }), | ||
| ); | ||
| app.use(express.json()); | ||
| const port = 4000; | ||
|
|
||
| const waitingRoom = []; | ||
|
|
||
| const chatHistory = [ | ||
| { | ||
| message: "Welcome to the channel.", | ||
| user: "System", | ||
| timestamp: new Date().getTime(), | ||
| }, | ||
| ]; | ||
|
|
||
| app.get("/", (req, res) => { | ||
| res.json(chatHistory); | ||
| }); | ||
|
|
||
| app.get("/messages", (req, res) => { | ||
| const since = parseInt(req.query.since); | ||
|
|
||
| if (isNaN(since)) { | ||
| return res.json(chatHistory); | ||
| } | ||
| const newMessages = chatHistory.filter(({ timestamp }) => timestamp > since); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An approach to reduce the amount of data sent in the response. Nicely done! |
||
|
|
||
| if (newMessages.length > 0) { | ||
| return res.json(newMessages); | ||
| } | ||
|
|
||
| const callback = (message) => res.json([message]); | ||
| waitingRoom.push(callback); | ||
|
|
||
| const seconds = 25; | ||
| const milliseconds = 1000; | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| const index = waitingRoom.indexOf(callback); | ||
| if (index !== -1) { | ||
| waitingRoom.splice(index, 1); | ||
| res.send([]); | ||
| } | ||
| }, seconds * milliseconds); | ||
|
Comment on lines
+39
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took me quite a while to figure out how the "waiting room" logic works. I think it is over complicated. These scenarios may be difficult to reproduce consistently, but there is a possibility that the "waiting room" logic could break under the following conditions:
No change needed. |
||
| }); | ||
|
|
||
| app.post("/", (req, res) => { | ||
| try { | ||
| let { message, user } = req.body; | ||
| if (!message?.trim() || !user?.trim()) { | ||
| res.status(406).json({ error: "Empty message or user are not allowed." }); | ||
| return; | ||
| } else { | ||
| const newTimestamp = new Date().getTime(); | ||
| const newMessage = { | ||
| message: message, | ||
| user: user, | ||
| timestamp: newTimestamp, | ||
| }; | ||
| chatHistory.push(newMessage); | ||
|
|
||
| while (waitingRoom.length > 0) { | ||
| const callback = waitingRoom.pop(); | ||
| callback(newMessage); | ||
| } | ||
| res.status(201).send("sent"); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Failed to parse body as JSON: ${error}`); | ||
| res.status(400).json({ error: "Expected body to be JSON." }); | ||
| return; | ||
| } | ||
| }); | ||
|
|
||
| app.listen(port, () => { | ||
| console.log(`chatApp server is listening on port: ${port}`); | ||
| }); | ||
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.
Could consider moving these declarations before
const app = express();