So I am trying to add my response.fee value from my express file into another JS file that includes specific functions that update my front end app.
Here is the JS code I am trying to add the express response in. Is this possible?
import response from "express";
window.menuItems = 0;
window.tips = 0;
const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");
document.addEventListener("click", ({ target }) => {
if (target.className === "food" && target.checked) {
window.menuItems += parseInt(target.value);
} else {
return
}
tipTotal.textContent = `$${(window.tips / 100).toFixed(2)}`;
orderTotal.textContent = `$${(
Number(window.menuItems) +
Number(window.tips) / 100
).toFixed(2)}`;
console.log(response.fee); //I want to get this value from my express file
});
So I can update the orderTotal code to say this
orderTotal.textContent = `$${(
Number(window.menuItems) +
Number(window.tips) + response.fee / 100
).toFixed(2)}`;
Is this something you can do with express? Or it's not possible?
I'm not so sure whether you're dealing with server-side rendering in your Express application or not but:
fetch (https://developer.mozilla.org/en-US/docs/Web/API/fetch for more details).ejs or pug and pass that value stored in response.fee to the res.render function in your Express app (res.render(<FILE_TO_RENDER>, { /* object with values to be passed to the file */ }).