Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Mickey Haile -Node-Coursework-Week2 #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nowadays you can just use express.json() (turns out the express package now includes bodyParser by default, and so express.json() is the same thing as bodyParser.json in middleware - this was news to me 😅 )


const app = express();

Expand All @@ -20,4 +21,46 @@ app.get("/", function (request, response) {
response.sendFile(__dirname + "/index.html");
});

app.get("/messages/search", (req, res) => {
const { term } = req.query;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always nice to try and cover off edge cases and handle them; e.g. what if term was an empty string, or undefined? How would you handle that?

console.log(term);
const filterMessages = messages.filter((message) =>
message.text.toLowerCase().includes(term.toLowerCase())
);
console.log(filterMessages);
res.send(filterMessages);
});

app.get("/messages", (req, res) => {
res.json(messages);
});

app.get("/messages/:id", function (req, res) {
const id = req.params.id;
messages = messages.filter((message) => message.id === Number(id));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To convert to an integer, use parseInt rather than the Number constructor - in this case it'd work out the same, but we tend to prefer parseInt as you can pass in a second parameter to specify the numeral system to parse the string as (e.g. hexidecimal, decimal, oct, etc)

res.status(200).send(messages);
});

app.get("/messages/latest", (req, res) => {
res.json(messages.slice(-10));
});

app.post("/messages", (req, res) => {
const { from, text } = req.body;
const ourMessageObject = {
id: Date.now(),
from,
text,
timeSent: new Date().toLocaleDateString(),
};
messages.push(ourMessageObject);
res.send("Message received successfully.");
});

app.delete("/messages/:id", (req, res) => {
const id = req.params.id;
messages = messages.filter((message) => message.id !== Number(id));
res.json(messages);
});

app.listen(process.env.PORT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would add a default value for this in case this env var is not set (e.g. const port = process.env.PORT ?? 3000)