Serving Static Fields
We use the following statement to serve static fields
app.use(express.static("public"));
app.use(express.static(path.join(__dirname,"public")));
index.js
const express = require("express");
const app = express();
const path = require("path");
const port = 8080;
app.use(express.static("public"));
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.get("/ig/:username", (req, res) => {
let { username } = req.params;
const InstaData = require("./data.json");
const data = InstaData[username];
if (data) {
res.render("insta.ejs", { data });
} else {
res.render("error.ejs");
}
});
app.listen(port, () => {
console.log(`server is listening on port ${port}`);
});
insta.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Instagram page</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>Welcome to the page of @<%=data.name%></h1>
<button>Follow</button>
<button>Message</button>
<p>Followers: <%= data.followers %> Following: <%=data.following %></p>
<!-- LOOP FOR POSTS -->
<% for(let post of data.posts){ %>
<img src="<%= post.image%>" />
<p>Likes: <%=post.likes%> comments:<%= post.comments %></p>
<% } %>
</body>
<script src="/app.js"></script>
</html>
style.css
img{
height: 100px;
width: 100px;
}
body{
background-color: aqua;
}
app.js
const btns = document.querySelectorAll("button");
for (btn of btns) {
btn.addEventListener("click", () => {
console.log("button was clicked");
});
}
Comments
Post a Comment