Reputation:
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>'
});
}
});
});
Upvotes: 0
Views: 654
Reputation: 99
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
Upvotes: 1