1
I am trying to implement a Loader in my application as follows:
1 - seto a variável isLoading para true
2 - faço as requests
3 - seto a variável isLoading para false
With this variable I can make an if in front-end (EJS) to show or not my gif of loading.
The problem is this, so I saw the only way to send a variable to the EJS is by using the req.render
and if I do this twice I need to change the state of the variable I end up taking an Exception:
(Ode:11752) Unhandledpromiserejectionwarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
How can I solve the problem? Follows code:
Express:
router.get('/backup', AuthCheck, async (req, res) => {
res.render('my-page', {
isLoading: true,
});
await saveProduct();
res.render('my-page', {
isLoading: false,
});
});
EJS:
<%if (isLoading) { %>
<div style="width: 100%;text-align: center">
<img style="width: 120px; height: 120px;" src="/imgs/loading.gif" />
</div>
<% } %>
This logic will never work because for each request
HTTP
, only one answer can be sent, which is precisely what therender
does. How about rethinking your problem? Instead of generating a div with a server-side loading, it would not be possible to use Javascript to generate and treat this client-side div?– Andre