INDEX ROUTE (GET /chats)

 INDEX.JS

// INDEX ROUTE
app.get("/chats", async (req, res) => {
  let chats = await Chat.find();
  res.render("index.ejs", { chats });
});

INDEX.EJS

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>All chats</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <h2>All Chats!</h2>
    <% for(let chat of chats) { %>
    <div class="Chat">
      <p>From:<i> <%= chat.from %></i></p>
      <div class="msg"><p>Message: <%= chat.msg %></p></div>
      <p>Received by: <i> <%= chat.to%></i></p>
    </div>
    <br />
    <% } %>
  </body>
</html>


STYLE.CSS

body{
    margin-left: 20px;
}

.Chat{
    background-color: lightgreen;
    display:inline-block;
    margin: 23px;
    border-radius:16px;
}
.msg{
    background-color: green;
    color: white;
    padding:5px;
    border-radius: 15px;
}

FOR CSS TO BE APPLIED

app.use(express.static(path.join(__dirname, "public")));









Comments

Popular posts from this blog

DELETE ROUTE

CREATE ROUTE

Schema type options