Thank you in advance for your assistance. I have a question that has been on my mind and it seems quite straightforward.
When making an app.get
request, I am fetching data from an external API using Express, and then sending both the JSON data and a Jade file through res.render
.
I have successfully managed to manipulate this data within Jade files and JavaScript embedded directly into the Jade file.
However, I am struggling to figure out how to work with this JSON data from a separate JavaScript file referenced by the HTML output.
For instance:
router.get('/dashboard', function (req, res, next) {
// Fetch and parse data from the external API
res.render('dashboard', {
data: data
});
});
The dashboard.jade file utilizes this data to perform various tasks.
if #{data.isNew} === true {do cool stuff}
I have also written JavaScript code that interacts with this data within the dashboard.jade file itself. However, I would prefer to write and incorporate this JavaScript in a separate file named scripts.js that is linked from the Jade file.
script(src='scripts.js')
Unfortunately, I am unable to access this data - I have attempted different syntaxes but have not succeeded.
Therefore, my query is this - how can I effectively handle JSON data that has been sent via res.render()
to a view, when working with a JS file referenced from that rendered view?