𝗗𝗮𝘆 𝟰𝟲 𝗼𝗳 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗠𝗘𝗥𝗡 𝗦𝘁𝗮𝗰𝗸
I am 46 days into my MERN stack journey.
Yesterday I set up Tailwind CSS. Today I learned the EJS templating engine.
Before today my HTML pages were static. They could not show changes from the server.
EJS allows server-side rendering. It creates HTML using live data from your backend. You write JavaScript logic directly inside your HTML.
Here is how I set it up today:
- I told Express to use EJS as the view engine.
- I created a views directory for my templates.
- I used the res.render method to send data to the frontend.
The code looks like this:
const express = require("express"); const app = express(); const users = require("./MOCK_DATA.json");
app.set("view engine", "ejs");
app.get("/users-list", (req, res) => { res.render("users", { allUsers: users }); });
app.listen(8000);
This method connects my data to my UI instantly.
Source: https://dev.to/ali_hamza_589ec7b3eb6688d/day-46-of-learning-mern-stack-15dh