猫鼬基于应用架构 [英] Mongoose-based app architecture
问题描述
的这不是一个具体的应用程序/ code的问题,它只是普通的应用程序架构。的
我试图理解来组织我的猫鼬应用正确的方法。由于我是新来的猫鼬,这就是我现在该怎么做:
I'm trying to understand proper way to organize my mongoose application. As I'm new to mongoose, that's how I do it now:
核心/ 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);
所以,这code的极度简化(甚至未测试)只是为了显示我目前的架构的想法。这不是一个具体的应用程序,只需像创建一个抽象的博客文章。因此多数民众赞成它是如何工作的:
So, this code was extremely simplified (even not tested) just to show my current architecture thoughts. It's not a concrete app, just something like creating an abstract blog post. So thats how it works:
app.js --> routes/post.js <--> core/db-layer.js
|
v
core/models.js <--> core/settings.js
这似乎有点画蛇添足了我。你可以建议更优化的应用程序的结构?谢谢你。
It seems a bit over superfluous for me. Could you suggest more optimal app structure? Thanks.
推荐答案
当我第一次进入的Node.js,防爆preSS和猫鼬我使用缩放我的code挣扎。
我的答案的目的是帮助别人谁的工作就不仅仅是一个简单的博客多了,但有一个更大的可扩展项目,以帮助。
When I first got into Node.js, Express and Mongoose I struggled with scaling my code. The intention of my answer is to help someone who's working on more than just a simple blog, but to help with an even larger scalable project.
- 我总是连接到数据库,我不打开和关闭连接在需要时
- 我用
index.js
为一个文件夹的根文件,就像我们会在其他的语言作 - 模型都保存在自己的文件和
要求()
D到模型/ index.js
文件 - 路线相似的机型,每个路由级别都有一个文件夹,这反过来又一个
index.js
文件。所以很容易安排类似http://example.com/api/documents/:id
。这也使得更多的意义,当一个经过的文件结构。
- I am always connected to the database, I do not open and close connections when needed
- I use
index.js
as the root file of a folder, just like we'd do in other languages - models are kept in their own documents, and
require()
d into themodels/index.js
file. - routes are similar to models, each route level has a folder, which has an
index.js
file in turn. So it's easy to arrange something likehttp://example.com/api/documents/:id
. It also makes more sense when one goes through the file structure.
下面是什么,我使用结构:
Here's the structure of what I use:
-- 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)
猫鼬/ 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
因此,对于每一个你对你的工作模式创建一个 model.js
的文件,并在模型/ index.js添加
上方。作为一个例子,我已经添加了一个用户
模式,但评论它。
So for every model that you work on you create a model.js
document, and add it in the models/index.js
above. As an example I've added a User
model but commented it out.
// 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
}
建议使用
- 型号:用于创建与文件交易的逻辑,即创建,更新,删除和搜索
- 路线:最少的编码,只有当我需要解析HTTP数据,创建模型的实例,然后我送查询到相关的模型 。
- 方法:对于更复杂的逻辑不直接涉及的模型。举个例子,我有我的地方存储所有我在我的应用程序使用的算法
算法/
文件夹中。
- models: for creating the logic that deals with the documents, i.e. creating, updating, deleting, and searching.
- routes: minimal coding, only where I need to parse http data, create instances of models, and then I send queries to the relevant model.
- methods: for the more complex logic that doesn't directly involve models. As an example, I have an
algorithms/
folder where I store all the algorithms that I use in my app.
希望这提供了更加清晰。这种结构是创造奇迹的我,我觉得很容易理解。
Hope this provides more clarity. This structure is working wonders for me as I find it easy to follow.
这篇关于猫鼬基于应用架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!