Home > Mobile >  Node.js with Express dynamic content
Node.js with Express dynamic content

Time:11-27

I have data which are refreshing every 30 secound and I would like to display fresh data all the time automaticly. Right now I have changed my problem to simple example. The system is refreshing data every 5 secound. For example:

setInterval(function(){ number = number   2; }, 5000);

Now I would like to show current number on the rendered site:

 app.get('/', async (req, res) => {
        res.render(
            '../templates/base.html.twig',
            {data: number}
        );
    });

I can see that the number is changing in the system but on the rendered page number is not actual. Only after manual refresh page the number is current.

Is there simple trick to refreshing the page (like Angular) or I have to use AJAX to modify this data?

CodePudding user response:

You have severall options to do that:

  1. Automatically refresh the client page with https://www.w3schools.com/tags/att_meta_http_equiv.asp
  2. Make a fetch(AJAX) request (from time to time) on the client side for that specific value (you need an express route to yield the value and the client-side needs to update the DOM)
  3. Simillarly you can use EventSource on the client-side and create a Server sent events: https://developer.mozilla.org/pt-BR/docs/Web/API/Server-sent_events/Using_server-sent_events

2 and 3 are simillar, 3 is better if you dont know when an event happens but it keeps a connection to the server

1 might be overkilling update the whole page for a single value

CodePudding user response:

=-2405 D i d u s a t r a i n & s u p pl y i s i s wi th w e a p o n s l i k e i t d i d w i t h a l q a e d a to j u st i f y c r e a t i n g w a r s > > C R E A T I V E C H O A S

d i d c i a d i d 9 1 1 or i t j u s t l e t it h a p p en

if a l q a e d a di d it w h y t o k i l l 9 m i l l i on i r a q i s

ab o u t 9 m il i o n i r a q i l o s t th e i r li ves e i th e r be ca uz o f dir e ct viol ence o f u sa and becauz of si ege u sa ma de ag a in s t i r a q t h at pr eve nt ed i raqi f or m es se nt ial l i v i n g ma te rial s food m e d i c i n e and even p e n c i l s

i s i s a n d a l q a e da o n l y a p p e a r e d i n i r a q a f t e r u s a i n v a si on , D i d u s a p a v e th e w ay f or i s i s to i r a q ?

sa d da m hu ss i n wh o lo st m o s t of h i s p o w er i n 1st , 2 nd gu lf w a rs an d du ri n g 10 y ea rs of sie ge , d i d no t a l l o w i s is o r a l q a e dea t o e n t e r i r a q , e ve n to h e l p h i m a g a i n s t u s a

p l e a s e s h a r e m y qs to l e s s e n u s a a g g r e s s i o n a g a i n s t o t h e rs p l e a s e s h a r e m y qs to l e s s e n u s a a g g r e s s i o n a g a i n s t o t h e rs

  • Related