From 03c79211cefd70f00ec42c5da57a32c873e3ca47 Mon Sep 17 00:00:00 2001 From: mickeyhaile2 <108958468+mickeyhaile2@users.noreply.github.com> Date: Wed, 5 Apr 2023 20:05:16 +0100 Subject: [PATCH] updated --- server.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/server.js b/server.js index 9e38fadd..66d4d028 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ const express = require("express"); const cors = require("cors"); +const bodyParser = require("body-parser"); const app = express(); @@ -20,4 +21,46 @@ app.get("/", function (request, response) { response.sendFile(__dirname + "/index.html"); }); +app.get("/messages/search", (req, res) => { + const { term } = req.query; + 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)); + 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);