Here, we will write a program to access a function in our ejs page.
server.js file in root directory
const app = require("express")()
const http = require("http").createServer(app)
const ejs = require("ejs")
const funExample = require("./functionExample")
app.set("view engine", "ejs")
const port = process.env.PORT || 3000
http.listen(port, function () {
app.get("/", async function (request, result) {
const html = await ejs.renderFile("views/index.ejs", {
funExample:funExample
}, {
async: true
})
result.send(html)
return
})
})
functionExample.js file in root directory
module.exports = {
myFunc() {
return "My function value."
}
}
index.ejs file in views directory
<%= funExample.myFunc() %>