简体   繁体   English

Mongoose连接在Express.js应用程序中自动共享

[英]Mongoose connection automatically shared in Express.js application

I'm developing a RESTful API using Express.js and Mongodb (and the mongoose module for accessing to the db with Schemas). 我正在使用Express.js和Mongodb(以及用于使用Schemas访问数据库的mongoose模块)开发RESTful API。 I was looking for the best way of sharing the connection to the Mongo database across the entire Express app in order to not to have a new connection for every single request (It's advised by the web and by other answers on Stackoverflow). 我一直在寻找在整个Express应用程序中共享Mongo数据库连接的最佳方式,以便不为每个请求建立新的连接(Web建议和Stackoverflow上的其他答案)。 I have read about different solutions and I decided to make a try using this architecture: 我已经阅读了不同的解决方案,我决定尝试使用这种架构:

In server.js (main app file) I have something like this: server.js (主应用程序文件)中我有这样的事情:

var express = require("express");
var bodyParser = require("body-parser");
var logger =  require("morgan");
var configuration = require("./configuration.js");
var fs = require("fs");
var HTTPresponses = require("./responses.js"); 
var mongoose = require("mongoose");

require("./models/user_model.js");

var Users = mongoose.model("Users");

//Connect to Db 
mongoose.connect("mongodb://127.0.0.1/test");

//Rest of the code here

As you can see I create a new connection to the database using mongoose and then I require user_module that is something like this: 正如您所看到的,我使用mongoose创建了与数据库的新连接,然后我需要user_module,如下所示:

var UserSchema = new Schema({
    name : {type: String},
    surname : {type: String},
    username : {type: String, unique: true, required: true}     
    }

,{
    collection : "Users"
});

mongoose.model("Users", UserSchema);

This way the UserSchema is compiled directly in the mongoose object. 这样,UserSchema直接在mongoose对象中编译。

And now is the Question : I noticed that every model compiled in server.js and the connection to the db are available in every middleware that I use just requiring the mongoose module"... " Why does it work? 现在是问题 :我注意到在server.js中编译的每个模型以及与db的连接都可以在我使用的每个中间件中使用,只需要mongoose模块“...” 为什么它可以工作? "

So... if I have a middleware that needs to access to the db the only thing I have to do is to require the moongose module and retreive models I need simply calling: 所以...如果我有一个需要访问数据库的中间件,我唯一需要做的就是需要moongose模块和我需要简单调用的retreive模型:

var MY_MODEL = mongoose.model("NAME_OF_THE_MODEL_COMPILED_in_serve.js");

I think this is a good thing but I have no idea about the why it is working. 我认为这是一件好事,但我不知道它为什么会起作用。 I mean... I have read tons of Questions and Answers talking about the best way to obtain this result but I have never read about this way. 我的意思是......我已经阅读了很多问题和答案,谈论了获得这个结果的最佳方法,但我从未读过这种方式。

I have also read the documentation about Express.js ... but nothing. 我还阅读了有关Express.js的文档......但没有。

Is it maybe related with the caching system of Express ? 它可能与Express的缓存系统有关吗?

Is it a safe way to share a database connection across the Express App? 它是一种跨Express App共享数据库连接的安全方式吗?

Thanks in advance, 提前致谢,

Luca 卢卡

As server.js is the main entry point of your application, that is the first place where node sees the mongoose module required. 由于server.js是应用程序的主要入口点,因此这是节点看到所需的mongoose模块的第一个位置。

Any update to the mongoose module instance is reflected in that particular instance. mongoose模块实例的任何更新都会反映在该特定实例中。

For example, if you require mongoose in your server.js file and in your user.js model file, you will only make changes to the same mongoose object. 例如,如果您需要mongooseserver.js文件,并在您的user.js模型文件,你只会更改相同的mongoose的对象。

This is due to the way CommonJS modules are cached throughout an application. 这是因为CommonJS模块在整个应用程序中的缓存方式。

If you would like to read more about the way modules are being cached, I recommend the following two sources: 如果您想了解有关模块缓存方式的更多信息,我建议您使用以下两个来源:

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

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