0

I am creating application which has dynamic several parts, like

  • header
    • menu 1
    • menu 2
  • content
  • sidebar
  • footer

Each part is fetching value from the database. right now what happened is when my page loads it is querying database to fetch the menu1, menu2, sidebar and footer, but which is actually fixed, and do not need database request once it loaded. here is the sudo code.

router.get('/', function(req, res, next) {
/*get menu 1*/
/*get menu 2*/
/*get menu content*/
/*get menu sidebar*/
/*get menu footer*/
/*render template*/
}

I am beginner to nodejs, can any one light up how can I save the database request? sample code will be helpful.

iGyan
  • 75
  • 8
  • http://stackoverflow.com/questions/4529586/render-basic-html-view-in-node-js-express You can render in your html format or send the dynamically generated html as a file using express sendFile – Shankar Shastri Sep 12 '16 at 17:10
  • plain html will not work for me, as menu and side bar is dynamic – iGyan Sep 12 '16 at 17:12
  • Based On your various routes you need to create html elements an send the response. http://blog.semmy.me/post/46435273508/using-express-to-serve-static-content-and-dynamic – Shankar Shastri Sep 12 '16 at 17:14
  • can you elaborate on what is fixed and what is dynamic? – jollarvia Sep 12 '16 at 17:22
  • you say something is fixed but you also say everything is dynamic in the question – jollarvia Sep 12 '16 at 17:25
  • @jollarvia on first request when the page is loaded it will be loaded then on click on link it will again send get request, but the menu is loaded it will not change, so it is fixed for second req. – iGyan Sep 12 '16 at 17:31
  • will the database ever be updated with new values? – jollarvia Sep 12 '16 at 17:32
  • yes, it will update, but when it will update it should only append new content – iGyan Sep 12 '16 at 17:37
  • Why not have only content in database and have everything else in template? – jollarvia Sep 12 '16 at 17:45
  • Can you use promises? Or callbacks? So , your final template will be rendered only when all db requests for the different elements are done. – linderman Dec 31 '16 at 15:18

1 Answers1

0

First of all, why do you need to query database for menu and footer. Most of the time these parts are plain html and can be included directly included into the view or template.

Usually, content is the only part that is changed from one page to another. You even mentioned that menu, sidebar and footer are fixed. So why not include them in the View directly.

If you absolutly need to get them from database then get the contents of menu, sidebar and footer from database on the first run and save them to session variables.

iiR
  • 707
  • 1
  • 6
  • 21