NW6 | Rabia Avci | Module Servers | [TECH ED] Chat Server API Project | Week 2#166
NW6 | Rabia Avci | Module Servers | [TECH ED] Chat Server API Project | Week 2#166RbAvci wants to merge 9 commits intoCodeYourFuture:mainfrom
Conversation
illicitonion
left a comment
There was a problem hiding this comment.
This looks great, good job!
chat-server/server.js
Outdated
|
|
||
| // GET a specific message by id | ||
| app.get("/messages/:id", (req, res) => { | ||
| const message = messages.find((p) => p.id === parseInt(req.params.id)); |
There was a problem hiding this comment.
How many times will this call the parseInt function? If you call parseInt on the same value multiple times, will it return different values? Can you think of a way to call it less often?
There was a problem hiding this comment.
To call parseInt less often we can parse the ID outside of the find function and store it in a variable 👍 Thanks!
chat-server/server.js
Outdated
|
|
||
| app.post("/messages", (req, res) => { | ||
| const newId = (lastId += 1); | ||
| if (!req.body.from) return res.status(422).json({ message: "From field is required" }); //A 422 status code indicates that the server was unable to process the request because it contains invalid data. |
There was a problem hiding this comment.
Even though your code here is correct and works, a lot of programmers will always add {}s around if bodies, even if they're just one statement - have a read of https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html for some explanation as to why :)
|




Learners, PR Template
Self checklist
Changelist
Some notes for my PR:
An Express.js server for a basic chat system:
Used Middlewares: like express.urlencoded, express.json, and cors to handle different types of requests and enable Cross-Origin Resource Sharing (CORS).
For Data: an array called messages to act as the data store for chat messages.
Routes:
Root Route: Serves index.html at the root URL ("/").
GET /messages: Retrieves all messages.
GET /messages/search: Filters messages by text query parameter.
GET /messages/latest: Retrieves the latest 10 messages.
GET /messages/:id: Retrieves a specific message by ID.
POST /messages: Adds a new message.
PUT /messages/:id: Updates a message by ID.
DELETE /messages/:id: Deletes a message by ID.