0

I need node.js to dynamically change the content of website which is fetched from database when page refreshes.

So, what I want is that when web page refreshes the images that user has uploaded to database shows on web page.

Whenever a user uploads a picture to database it's address is added to database and picture also gets uploaded on server, but the image doesn't show right on webpage until i restart the node.js server.

Only database rows gets updated when page reloads.

below is the code:

app.get('/',(req,res)=>{
    connection.query("SELECT * FROM images",(error,rows,fields)=>{
        if(!!error){
            console.log("query not successfull");
        }
        else{
            console.log("connected to images table");
            for(var i=rows.length-1;i>=0;i--){
                adddress.push(rows[i].image);

                imageRows=rows.length;



            }

            res.render('index',{
                address:adddress,

                imageRowsSize:imageRows,

                linebreaker:'<br>'  
            });


        }
    });

});


Lucas Meine
  • 1,524
  • 4
  • 23
  • 32

2 Answers2

1

If I understand this correctly, wouldn't your client-side request the page refresh/reload and then do a call to your nodejs api to fetch on that event? For example, in Angular there is a reload option for when a navigation event (refresh on same url) happens where then you can do a fetch() for data.

Something like this(angular article): https://blog.angularindepth.com/refresh-current-route-in-angular-512a19d58f6e

or what about having ajax do a reload for you on success? Example: jQuery Refresh/Reload Page if Ajax Success after time

Regarding when the page refreshes by the user, check this answer: Ajax call when click on Browser refresh Button

user11317802
  • 79
  • 11
  • i'm actually not using any front end framework. i am using ejs as my template though.can u link me how to make client side requests without using any other front end framework? –  Nov 11 '19 at 14:23
  • @dark_3nergy, I edited my answer to see if it could help – user11317802 Nov 11 '19 at 15:05
0

Just reload the page by using

res.redirect('/');
SomeCoder
  • 275
  • 1
  • 19