This is my app.js
var products = [
{description: "Hi"},
{description: "Hi2"}
]
app.get('/', function (req, res){
res.render("home.ejs");
})
app.get('/checkout', function (req, res){
res.render('checkout.ejs', {products:products});
});
app.post('/checkout', function(req, res){
var description = req.body.description;
var newProduct = {description};
products.push(newProduct);
res.redirect('/');
});
This is my home.ejs
<form action="/checkout" method = "POST">
<p class = "item-description">LP Snake Pocket Tee</p>
<input type="hidden" name = "description" class = "item-description-post">
<div class="btn-group btn-add mr-2" role="group" aria-label="Second group">
<button class="btn btn-success btn-add-cart">ADD TO CART</button>
</div>
</form>
And this is my hoje.js file, which just basically fills my input value with what I want, so I can send that as POST input
var item = $(".item-description")[0].innerHTML;
var postInput = $(".item-description-post").val(item);
Now I'm my checkout.ejs file I can do something like this, and print all my products (descriptions only so far)
<% products.forEach(function(product){ %>
<p> <%= product.description %> </p>
<% }); %>
My question is, how can I do the same thing, but in a separate .js file?
I do have a javascript file connected with my checkout.ejs
<!-- JAVASCRIPT -->
<script type = "text/javascript" src="./checkout.js"></script>
But that file can not access my array 'products'. Which makes sense, because my back end is only sending that data to my ejs file, so basically, how do I send that data 'products' from my app.js file to my checkout.js file? :)))