-
-
Notifications
You must be signed in to change notification settings - Fork 55
Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 2 | Chat App #84
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
90e2c3b
1518e8b
c310b9c
9b80f19
85ad07e
b393959
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,2 @@ | ||
| node_modules/ | ||
| .DS_Store |
| 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 = [ | ||
| { | ||
| 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; | ||
|
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. It's good practice to validate all request inputs. Note: A more robust implementation
In this exercise, let's assume the request is always coming from the code in |
||
|
|
||
| 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
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. It's a bit inconsistent that |
||
| time: Date.now(), | ||
| likes: 0, | ||
| dislikes: 0, | ||
| }; | ||
|
|
||
| messages.push(newMessage); | ||
| while (callbacksForNewMessages.length > 0) { | ||
| const callback = callbacksForNewMessages.pop(); | ||
| callback([messages[messages.length - 1]]); | ||
|
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. Wouldn't sending |
||
| } | ||
| 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}`); | ||
| }); | ||
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.
Better to declare global constants and variables before
const app = express()because lines 5-6 are already logic to setup the server (app).