I am starting to learn Node JS. I am trying to set global values available to all templates using below code.
const express = require("express");
const expressEdge = require("express-edge");
const homePageController = require("./controllers/homePage");
const app = express();
app.use(express.static("public"));
app.use(expressEdge);
app.set("views", `${__dirname}/views`);
app.use("*", async (req, res, next) => {
import("edge.js")
.then(({ default: edge }) => {
console.log("Value set to name");
edge.global("name", "TEST");
})
.catch((error) => {
console.error("Error importing edge.js:", error);
});
next();
});
app.get("/", homePageController);
app.listen(4000, () => {
console.log("App listening on port 4000");
});
In the app.edge file I am just trying to display the value
<div>{{name || 'NO NAME'}}</div>
This just displays ‘NO NAME’ since name value is undefined. As per the edge.js documentation edge.global(‘some-key’,’some-value’) should make the value available to all template. Edge.js version I have installed is “edge.js”: “^6.0.2”, and the version of node I have is “18.14.2”. What am I missing here ?