基于Mongoose的应用程序体系结构

这不是一个具体的应用程序/代码问题,只是关于通用的应用程序架构。

我试图理解正确的方式来组织我的mongoose应用程序。 因为我是mongoose新手,现在就是这样做的:

核心/ settings.js

var mongoose = require('mongoose'); exports.mongoose = mongoose; mongoose.connect('mongodb://localhost/blog'); exports.db = mongoose.connection; 

核心/ models.js

 settings = require("./settings"); // post schema var postSchema = settings.mongoose.Schema({ header: String, author: String, text: String }) //compiling our schema into a Model exports.post = settings.mongoose.model('post', postSchema) 

芯/ DB-layer.js

 settings = require("./core/settings"); models = require("./core/models"); exports.function = createAndWriteNewPost(function(callback) { settings.db.on('error', console.error.bind(console, 'connection error:')); settings.db.once('open', function callback() { new models.post({ header: 'header', author: "author", text: "Hello" }).save(function(err, post) { callback('ok'); }); }); }); 

路线/ post.js

 db = reqiure("../core/db.js") exports.get = function(req, res) { db.createAndWriteNewPost(function(status){ res.render('add_material', { //blah blah blah }); }); }; 

app.js

 var post = require ('routes/post.js') ... app.get('/post', post.get); 

所以,这个代码非常简单(甚至没有testing),只是为了展示我当前的架构思想。 这不是一个具体的应用程序,只是像创build一个抽象的博客文章。 那么它是如何工作的:

 app.js --> routes/post.js <--> core/db-layer.js | v core/models.js <--> core/settings.js 

这对我来说似乎有点过分了。 你可以build议更优化的应用程序结构? 谢谢。

当我第一次进入Node.js,Express和Mongoose时,我正在努力缩放我的代码。 我的答案的目的是帮助不仅仅是一个简单的博客工作的人,而是帮助一个更大的可扩展项目。

  • 我总是连接到数据库,我不打开和closures需要的连接
  • 我使用index.js作为文件夹的根文件,就像我们在其他语言中做的一样
  • 模型保存在自己的文档中,并且require() d到models/index.js文件中。
  • 路由类似于模型,每个路由级别都有一个文件夹,它依次具有一个index.js文件。 所以很容易安排像http://example.com/api/documents/:id这样的东西。 当通过文件结构时,这也更有意义。

这是我使用的结构:

 -- app.js -- models/ ---- index.js ---- blog.js -- mongoose/ ---- index.js -- routes/ ---- index.js ---- blog/index.js -- public/ -- views/ ---- index.{your layout engine} => I use Jade.lang -- methods/ ---- index.js => use if you'd rather write all your functions here ---- blog.js => can store more complex logic here 

app.js

 var db = require('./mongoose'), express = require('express'); // note that I'm leaving out the other things like 'http' or 'path' var app = express(); // get the routes require('./routes')(app); // I just require routes, without naming it as a var, & that I pass (app) 

mongoose/ index.js

 // Mongoose connect is called once by the app.js & connection established // No need to include it elsewhere var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/blog'); // I have just connected, and I'm not exporting anything from here 

车型/ index.js

 // Logic here is to keep a good reference of what's used // models Blog = require('./blog'); // User = require('./user'); // exports exports.blogModel = Blog.blogModel; // exports.userModel = User.userModel; 

车型/ blog.js

因此,对于您工作的每个模型,创build一个model.js文档,并将其添加到上面的models/index.js 。 作为一个例子,我已经添加了一个User模型,但评论出来。

 // set up mongoose var mongoose = require('mongoose'); var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var BlogSchema = Schema({ header: {type: String }, author: {type: String }, text: {type: String }, _id: { type: ObjectId } // not necessary, showing use of ObjectId }); Blog = mongoose.model('Blog', BlogSchema); // the above is necessary as you might have embedded schemas which you don't export exports.blogModel = Blog; 

路线/ index.js

 module.exports = function(app) { app.get('/', function(req, res) { // do stuff }); require('./blog')(app); // other routes entered here as require(route)(app); // we basically pass 'app' around to each route } 

路线/博客/ index.js

 module.exports = function(app) { app.get('/blog', function(req, res) { // do stuff }); require('./nested')(app); // this is for things like http://example.com/blog/nested // you would follow the same logic as in 'routes/index.js' at a nested level } 

build议使用

  • 模型:用于创build处理文档的逻辑,即创build,更新,删除和search。
  • 路由:最小编码,只有在我需要parsinghttp数据,创build模型的实例,然后我发送查询到相关的模型。
  • 方法:对于不直接涉及模型的更复杂的逻辑。 作为一个例子,我有一个algorithms/文件夹,用于存储我在应用程序中使用的所有algorithm。

希望这提供更多的清晰度。 这种结构对我来说是奇迹,因为我觉得很容易遵循。

这几乎是我如何去做,有一些不同之处:

  • 我不认为你可以在你的function在db层的开放侦听器。 当我使用类似于你的持久连接时,我通常会做的是在db打开处理程序中启动应用程序本身。 如果您不想使用持续连接,请在db层函数中使用createConnection,并确保在调用callback之前将其closures。 我不确定自己是否清楚。 让我知道你是否想要一个代码示例。
  • 这更像是一个通用的node.js提示,但是我将数据库连接string和其他configuration保存在json文件中,并在需要的地方使用。 之后你可能不需要另外的settings.js文件。
  • 您还可以使用模式函数( http://mongoosejs.com/docs/api.html#schema_Schema-method )将一些应用程序function编码到模型本身中。