Search code examples
javascriptnode.jsexpress-handlebars

How to reload page with new data in Node.js


I'm using express-handlebars as my template engine and I have created an 'Article' template. I have 5 articles in total. I need to show the user each article but randomly. How would I create a 'Next' button that could reload the page with a new article?

I have thought of using a cookie in the browser and implementing some sort of array within it to decide the next article to be shown (the array will be randomised). As with loading the articles, I have thought of creating a new page for each article and then redirecting the user to a random page when the click 'next', but that wouldn't be making much use of the template engine.

I don't have code to show as I'm looking for a concept that would work.

I want the user to open my website, be shown a random article, click next and another article appear. I don't want each user to have the same sequence of articles (obviously with many users this is impossible but I'd like to minimise it).


Solution

  • For 5 articles,
    1. track the articles already read by a user via cookies
    2. add a query parameter to the article template link like randon=1 so as to respond back with a random article and another parameter not_in=*already read article ids* to exclude these ones.