dimanche 4 mars 2018

How to share global sidebar/footer on every route (webpage) in Koa/ Express?

i have written a Website with Node.js and Koa/Express.

i have this routes in my app.js:

router.use('/about/', about.routes()
router.use('/blog/', blog.routes()
router.use('/imprint/', imprint.routes())
router.use('/', index.routes())
router.use('/user/:id/', user.routes())
app.use(router.routes())

and every page of this route has the same global sidebar with information about the latest blogs. Now i have a query for this (blogs on sidebar) on every route.

query database for blogs -> save result to variable -> render page with sidebar

Is it possible to to query this once on every request and set the result to a global variable and share this to the routes?

Example:

1: User visit the website
2: Koa query the results for the the sidebar (route: /)
3. User see the sidebar on the webpage
4: User click on a blog (new page route: /blog/)
5: Koa share the result of the sidebar (Point 2)
6: User see the sidebar on the webpage

...




Aucun commentaire:

Enregistrer un commentaire