简体   繁体   English

模块化Node.js Express MongoDB应用程序的更好方法

[英]Better way for modular nodejs express mongodb app

I am creating application which has dynamic several parts, like 我正在创建具有动态多个部分的应用程序,例如

  • header 标头
    • menu 1 菜单1
    • menu 2 菜单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. 现在发生的事情是我的页面加载时正在查询数据库以获取menu1,menu2,侧边栏和页脚,但实际上是固定的,加载后不需要数据库请求。 here is the sudo code. 这是sudo代码。

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? 我是nodejs的初学者,可以点亮任何一个如何保存数据库请求吗? sample code will be helpful. 示例代码将很有帮助。

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. 在大多数情况下,这些部分都是纯HTML,可以直接包含在视图或模板中。

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. 如果绝对需要从数据库中获取它们,则在第一次运行时从数据库中获取菜单,侧边栏和页脚的内容,并将其保存到会话变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM