WillemvanderVeen WillemvanderVeen - 1 year ago 176
Javascript Question

Navigation using express and EJS

Having a issue figuring out how to navigate between EJS pages effectively:

File directory:



enter image description here

I want to go from my index.ejs page to the about.ejs page. This is the code for my index.ejs page which is currently not navigating properly:

index.ejs:



<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1> <a href="about.ejs"> This is a link to about page</a></h1>
</body>
</html>


app.js server:



const express = require("express");
const path = require('path');
const app = express();

app.set("views", path.resolve(__dirname, "views"));
app.set("view engine", "ejs")

app.get("/", (req, res) => {
res.render("index")
});

app.use(express.static('public'));

app.listen(3000);


What could I enter in the href to properly reference the dynamic about.ejs file?
I already know that I can reference static files from my public folder but I want to refernce the dynamic ejs folder. If not possible any solution which provides the same functionality will also do.

Answer Source

You should render about.ejs template to use it on the client. To do that, you need to create a new route:

app.get("/about", (req, res) => {
  res.render("about");
});

To open it use /about path.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download