-
-
Notifications
You must be signed in to change notification settings - Fork 76
NW6 | Areeb Sattar | Module Servers | Chat server API project | Week 2 #179
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
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { fileURLToPath } from "url"; | |
|
|
||
| const app = express(); | ||
|
|
||
| app.use(express.json()); | ||
| app.use(cors()); | ||
|
|
||
| // Get __dirname in ES module | ||
|
|
@@ -25,6 +26,31 @@ app.get("/", (request, response) => { | |
| response.sendFile(__dirname + "/index.html"); | ||
| }); | ||
|
|
||
| app.post("/messages", (req, res) => { | ||
| const newMessage = req.body; | ||
| messages.push(newMessage); | ||
| res.send("Message added"); | ||
| }); | ||
|
|
||
| app.get("/messages", (req, res) => { | ||
| res.send(messages); | ||
| }); | ||
|
|
||
| app.get("/messages/:id", (req, res) => { | ||
| const messageId = parseInt(req.params.id); | ||
| const message = messages.find((obj) => obj.id === messageId ); | ||
| if (!message) return res.status(404).json({ message: "Message does not exist" }); | ||
|
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. Even though your code here is correct and works, a lot of programmers will always add |
||
| res.json(message); | ||
| }); | ||
|
|
||
| app.delete("/messages/:id", (req, res) => { | ||
| const index = messages.findIndex((obj) => obj.id === parseInt(req.params.id)); | ||
| if (index === -1) return res.status(404).json({ message: "No message to delete" }); | ||
|
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. Even though your code here is correct and works, a lot of programmers will always add |
||
|
|
||
| messages.splice(index, 1); | ||
| res.json({message : "Message successfully deleted"}); | ||
| }) | ||
|
|
||
| app.listen(process.env.PORT, () => { | ||
| console.log(`listening on PORT ${process.env.PORT}...`); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Good grasp of Create (POST) function!
However, does
reg.bodycontain all properties required in the message? (See Data Model)