-
-
Notifications
You must be signed in to change notification settings - Fork 75
NW6 | Fikret Ellek| Module Servers | Chat server API project | Week 2 #180
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 6 commits
9f063c8
560e178
494e826
ab44be5
c2e00a3
56c10c8
466b948
f028124
d0596c3
272fb47
859c1fb
db21a7f
b210f54
ecfc28c
d046958
3741208
e0fd732
9de7d7c
e9a6afe
ac48efb
e8eab42
79e6e59
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 |
|---|---|---|
|
|
@@ -2,11 +2,13 @@ process.env.PORT = process.env.PORT || 9090; | |
| import express from "express"; | ||
| import cors from "cors"; | ||
| import path from "path"; | ||
| import bodyParser from "body-parser"; | ||
| import { fileURLToPath } from "url"; | ||
|
|
||
| const app = express(); | ||
|
|
||
| app.use(cors()); | ||
| app.use(bodyParser.json()); | ||
|
|
||
| // Get __dirname in ES module | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
|
@@ -21,8 +23,68 @@ const welcomeMessage = { | |
| //We will start with one message in the array. | ||
| const messages = [welcomeMessage]; | ||
|
|
||
| app.get("/", (request, response) => { | ||
| response.sendFile(__dirname + "/index.html"); | ||
| app.get("/", (req, res) => { | ||
| res.sendFile(__dirname + "/index.html"); | ||
| }); | ||
|
|
||
| app.post("/messages", (req, res) => { | ||
| const newMessage = req.body; | ||
| if ( | ||
| !newMessage.hasOwnProperty("text") || | ||
| !newMessage.hasOwnProperty("from") || | ||
| newMessage.from === "" || | ||
| newMessage.text === "" || | ||
| typeof newMessage.text != "string" || | ||
| typeof newMessage.from != "string" | ||
| ) { | ||
|
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. If you want to go one further step, you can create your validation fun and give single responsibility to it. It looks like this:
Author
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. I have modified the route function. Could you have a look please? |
||
| res.sendStatus(400).send("wrong input"); | ||
|
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. One suggestion for improvement is that instead of using
Author
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. Ahh I see, that makes more sense. Thank you. |
||
| } | ||
| newMessage["id"] = messages[messages.length - 1].id + 1; | ||
| newMessage["timeSent"] = new Date(); | ||
| messages.push(newMessage); | ||
| res.json(messages); | ||
| }); | ||
|
|
||
| app.get("/messages", (req, res) => { | ||
| res.json(messages); | ||
| }); | ||
|
|
||
| app.get("/messages/id/:id", (req, res) => { | ||
|
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. There is no right and wrong, but you generally don't need to include the word 'id' in the route path. you can simply use
Author
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. I really had a bad time fixing this 😆 when I tried to create another route (ex: |
||
| const messageId = req.params.id; | ||
| const searchedMessage = messages.find((message) => message.id == messageId); | ||
| res.json(searchedMessage); | ||
| }); | ||
|
|
||
| app.delete("/messages/id/:id", (req, res) => { | ||
|
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. Same as before!
Author
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. Can we set up a meeting for this issue? I couldn't find how to resolve dynamic and static path blocking |
||
| const messageId = req.params.id; | ||
| const deleteMessage = messages.find((message) => message.id == messageId); | ||
| messages.splice(messages.indexOf(deleteMessage), 1); | ||
| res.json(messages); | ||
| }); | ||
|
|
||
| app.get("/messages/search", (req, res) => { | ||
| const searchInput = req.query.text.toLowerCase(); | ||
| const filteredMessages = messages.filter((message) => | ||
| message.text.toLowerCase().includes(searchInput) | ||
| ); | ||
| res.json(filteredMessages); | ||
| }); | ||
|
|
||
| app.get("/messages/latest", (req, res) => { | ||
| const latestMessages = messages.slice(-10); | ||
|
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. I'm sure you can to this more dynamic instead of using hardcoded length 💯
Author
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. yes it makes more sense. I have updated the route with |
||
| res.json(latestMessages); | ||
| }); | ||
|
|
||
| app.patch("/messages/id/:id", (req, res) => { | ||
| console.log(req.params.id); | ||
| const messageId = req.params.id; | ||
| const updatedInfo = req.body; | ||
| const patchMessage = messages.find((message) => message.id == messageId); | ||
| messages[messages.indexOf(patchMessage)] = { | ||
| ...messages[messages.indexOf(patchMessage)], | ||
| ...updatedInfo, | ||
| }; | ||
| res.json(messages); | ||
| }); | ||
|
|
||
| app.listen(process.env.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.
Nice touch using
nodemon! This can save you a lot of time and effort during development. Well done!