SHOW route (GET / user)

 

INDEX.JS

app.get("/users", (req, res) => {
  let q = 'SELECT * FROM user';

  try {
  connection.query(q, ( err,users) => {
    if (err) throw err;
    res.render("show.ejs",{users});
  });
} catch (err) {
  console.log(err);
  res.send("error occured");
}
});
app.listen(port, () => {
  console.log(`listening to port ${port}`);
});

SHOW.EJS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>show users</title>
    <style>
    table,
    th,
    td{
        border: 1px solid black;
    }
    </style>
</head>

<body>
    <table>
    <tr>
        <th>Id</th>
        <th>username</th>
        <th>Email</th>
<% for(user of users){ %>
<tr>
    <td><%= user.id %></td>
    <td><%= user.username %></td>
    <td><%= user.EMAIL %></td>

</tr>
<%}%>
    </tr>
    <tr>

    </tr>
</table>
</body>
</html>


Comments

Popular posts from this blog

DATABASE RELATIONSHIPS

ROUTING (GET /) home route

Query Strings