-
-
Notifications
You must be signed in to change notification settings - Fork 76
NW6 | Rabia Avci | Module Servers | [TECH ED] Chat Server API Project | Week 2 #166
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 8 commits
807ec04
3330ba7
dd02cce
6f7b1e5
43aa9a7
12581f3
6814892
ad004f6
d0a1168
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 |
|---|---|---|
| @@ -1,28 +1,117 @@ | ||
| process.env.PORT = process.env.PORT || 9090; | ||
| import express from "express"; | ||
| import express, { json } from "express"; | ||
| import cors from "cors"; | ||
| import path from "path"; | ||
| import { fileURLToPath } from "url"; | ||
|
|
||
| const app = express(); | ||
|
|
||
| app.use(express.urlencoded({ extended: true })); | ||
| app.use(express.json()); | ||
| app.use(cors()); | ||
|
|
||
| // Get __dirname in ES module | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const welcomeMessage = { | ||
| id: 0, | ||
| from: "Bart", | ||
| text: "Welcome to CYF chat system!", | ||
| }; | ||
|
|
||
| //This array is our "data store". | ||
| //We will start with one message in the array. | ||
| const messages = [welcomeMessage]; | ||
|
|
||
| app.get("/", (request, response) => { | ||
| response.sendFile(__dirname + "/index.html"); | ||
| const messages = [ | ||
| { | ||
| id: 0, | ||
| from: "Bart", | ||
| text: "Welcome to CYF chat system!", | ||
| timeSent: new Date(), | ||
| }, | ||
| { | ||
| id: 1, | ||
| from: "test", | ||
| text: "test", | ||
| timeSent: new Date(), | ||
| }, | ||
| ]; | ||
|
|
||
| let lastId = 1; | ||
|
|
||
| app.get("/", (req, res) => { | ||
| res.sendFile(__dirname + "/index.html"); | ||
| }); | ||
|
|
||
| // Get all messages | ||
| app.get("/messages", (req, res) => { | ||
| res.send(messages); | ||
| }); | ||
|
|
||
| //Level 3 - more "read" functionality | ||
|
|
||
| app.get("/messages/search", (req, res) => { | ||
| console.log("here"); | ||
| const searchText = req.query.text; | ||
| if (!searchText) { | ||
| return res.status(400).json({ message: "Please provide a 'text' query parameter" }); | ||
| } | ||
| const filteredMessages = messages.filter((message) => message.text.includes(searchText)); | ||
| res.json(filteredMessages); | ||
| }); | ||
|
|
||
| app.get("/messages/latest", (req, res) => { | ||
| const latestMessages = messages.slice(-10); | ||
| res.json(latestMessages); | ||
| }); | ||
|
|
||
| // GET a specific message by id | ||
| app.get("/messages/:id", (req, res) => { | ||
| const message = messages.find((p) => p.id === parseInt(req.params.id)); | ||
| if (!message) return res.status(404).json({ message: "Message not found" }); | ||
| res.json(message); | ||
| }); | ||
|
|
||
| // POST a new message | ||
|
|
||
| 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. | ||
|
||
| if (!req.body.text) return res.status(422).json({ message: "Text field is required" }); | ||
|
|
||
| const timeSent = new Date(); // Adding timestamp | ||
| const message = { | ||
| id: newId, | ||
| from: req.body.from, | ||
| text: req.body.text, | ||
| timeSent: timeSent, | ||
| }; | ||
| lastId = newId; | ||
| messages.push(message); | ||
| res.status(201).json(message); | ||
| }); | ||
|
|
||
| // Level 5 - Optional - PUT to update a message | ||
|
|
||
| app.put("/messages/:id", (req, res) => { | ||
| const messageId = parseInt(req.params.id); | ||
| const messageIndex = messages.findIndex((message) => message.id === messageId); | ||
|
|
||
| if (messageIndex === -1) { | ||
| return res.status(404).json({ message: "Message not found for an update" }); | ||
| } | ||
| const updatedMessage = messages[messageIndex]; | ||
|
|
||
| if (req.body.text !== undefined) { | ||
| updatedMessage.text = req.body.text; | ||
| } | ||
|
|
||
| if (req.body.from !== undefined) { | ||
| updatedMessage.from = req.body.from; | ||
| } | ||
| res.json(updatedMessage); | ||
| }); | ||
|
|
||
| // DELETE a message | ||
|
|
||
| app.delete("/messages/:id", (req, res) => { | ||
| const index = messages.findIndex((p) => p.id === parseInt(req.params.id)); | ||
| if (index === -1) return res.status(404).json({ message: "Message not found for delete" }); | ||
|
|
||
| messages.splice(index, 1); | ||
| res.json({ message: "Message deleted" }); | ||
| }); | ||
|
|
||
| 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.
How many times will this call the
parseIntfunction? If you callparseInton 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To call parseInt less often we can parse the ID outside of the find function and store it in a variable 👍 Thanks!