如何构建我的快速应用程序,我只需要打开一个mongodb连接一次? [英] How can I structure my express app where I only need to open a mongodb connection once?

查看:121
本文介绍了如何构建我的快速应用程序,我只需要打开一个mongodb连接一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:请在回答之前阅读此帖的已编辑部分,可能会节省时间并回答我的一个问题。



我遇到的问题很简单,但我对这个整体来说非常新鲜,而且我正在解决如何在节点/ express应用程序中正确实现mongodb数据库连接的问题。



我使用express 3.x,并且基于我的应用程序布局围绕由作者express提供的这个项目:
https://github.com/visionmedia/express/tree/master/examples/blog



我有然而,制作博客的兴趣似乎是相当不错的。路线分开,一切都很好地组织。



我的问题是我可能有5-6个不同的路由js文件,每个路由js文件可能在1到15之间航线;那些路由1或15可能需要访问db。



所以我的问题是,做一个db.open(...)似乎是一个非常糟糕的想法,每一次我想查询数据库。我应该提到,我使用的是本地的mongo-db驱动程序(npm install mongodb)。



我还需要包含一个这样的文件:
http://pastebin.com/VzFsPyax



... in所有这些路由文件和我的所有模型文件。那么我也在处理几十个开放的连接。



有没有办法我可以用这样一种方式构建我的应用程序,我只能做一个连接在会议期间保持开放(每个请求都有一个新的会变坏)?



如果是,我该怎么做?如果您知道答案,请使用tj的博客应用程序(该帖子之前链接的一个)结构​​作为基础指南发布代码示例。基本上有一种路径和模型可以在分开的文件中使用db的方式,而不是db开放代码。



谢谢。



编辑



我在解决我的一个问题方面取得了一些进展。如果你看看tj的博客示例,他会在app.js中初始化他的路线,如下所示:

  require('./ routes /站点')(应用程序); 
require('./ routes / post')(app);

在路由js文件中,它开始如下:

  module.exports = function(app){

我今天早些时候发现一个项目,我看到有人在modules.exports调用 - > function(app,db)中传递了两个变量。那么想哇可以这么容易,我需要调整我的路线是(app,db)吗?是啊,好像是这样。



所以现在第1部分的问题已经解决了。我不需要在每个路由文件中使用连接样板的mongo.js文件。同时它足够灵活,我可以决定选择哪些路由文件传递数据库引用。这是标准的,没有下降的权利?



第2部分的问题(不幸的是重要的)仍然存在。



如何绕过每个查询所需的db.open(...),理想情况下,每个会话只能连接一次?

解决方案

真的很傻。在文档中,似乎db.open需要被包装在使用它的任何东西,但实际上你可以使用它没有回调。



所以答案是只需在数据库连接模块,app.js文件中或在何处决定设置数据库服务器/连接时执行一个db.open()。



只要你在使用它的文件中传递对db的引用,您将可以访问已打开的数据库连接,可以查询。


Note: Please read the edited portion of this post before answering, it might save you time and answers one of my questions.

The problem I'm having is pretty simple but I'm pretty new to this overall and I'm having issues figuring out how to implement a mongodb database connection properly in a node/express app.

I'm using express 3.x and am basing my app layout around this project supplied by the author of express: https://github.com/visionmedia/express/tree/master/examples/blog

I have no interest in making a blog however the way the app is structured appears to be quite nice. The routes are separated and everything is organized nicely.

My problem is I might have 5-6 different route js files and each route js file might have anywhere between 1 and 15 routes; of those routes 1 or 15 might want to access the db.

So my problem is it seems like a really terrible idea to do a db.open(...) every single time I want to query the db. I should mention at this point I'm using the native mongo-db driver (npm install mongodb).

I would also need to include a file like this: http://pastebin.com/VzFsPyax

...in all of those route files and all of my model files. Then I'm also dealing with dozens upon dozens of open connections.

Is there a way I can structure my app in such a way where I only make 1 connection and it stays open for the duration of the session (having a new one made every request would be bad too)?

If so, how can I do this? If you know the answer please post a code sample using tj's blog app (the one linked earlier in this post) structure as a base guide. Basically have a way where the routes and models can use the db freely while being in separate files than the db open code.

Thanks.

EDIT

I made some progress on solving one of my issues. If you look at tj's blog example he initializes his routes in the app.js like so:

require('./routes/site')(app);
require('./routes/post')(app);

And in the routes js file it starts like this:

module.exports = function(app){

I stumbled on a project earlier today where I saw someone pass 2 variables in the modules.exports call -> function(app, db). Then figured wow could it be that easy, do I need to just adjust my routes to be (app, db) too? Yeah, it seems so.

So now part 1 of the problem is solved. I don't have to require a mongo.js file with the connection boilerplate in every route file. At the same time it's flexible enough where I can decide to pick and choose which route files pass a db reference. This is standard and has no downside right?

Part 2 of the problem (the important one unfortunately) still exists though.

How can I bypass having to do a db.open(...) around every query I make and ideally only make a connection once per session?

解决方案

Really silly. In the documentation it seems like db.open requires to be wrapped around whatever is using it, but in reality you can use it without a callback.

So the answer is to just do a db.open() in your database connection module, app.js file or where ever you decide to setup your db server/connection.

As long as you pass a reference to the db in the files using it, you'll have access to an "opened" db connection ready to be queried.

这篇关于如何构建我的快速应用程序,我只需要打开一个mongodb连接一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆