Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 基于Mongoose的应用程序架构大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这不是一个具体的应用/代码问题,它只是常见的应用程序架构。

我想了解正确的方式来组织我的mongoose应用程序。因为我是新来的猫头鹰,这就是我现在做的事情:

核心/ setTings.js

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

核心/ models.js

@H_197_11@setTings = require("./setTings"); // post scheR_420_11845@a var postscheR_420_11845@a = setTings.mongoose.scheR_420_11845@a({ header: String,author: String,text: String }) //compiling our scheR_420_11845@a into a Model exports.post = setTings.mongoose.model('post',postscheR_420_11845@a)

芯/ DB-layer.js

@H_197_11@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

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

app.js

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

所以,这个代码非常简化(甚至没有测试)只是为了显示我目前的架构思路。这不是一个具体的应用程序,就像创建一个抽象博客文章一样。那么它是如何工作的:

@H_197_11@app.js --> routes/post.js <--> core/db-layer.js | v core/models.js <--> core/setTings.js

对我而言似乎有点多余。你能建议更好的应用程序结构吗?谢谢。

解决方法

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

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

以下是我使用的结构:

@H_197_11@-- 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

@H_197_11@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

@H_197_11@// 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

@H_197_11@// 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

因此,对于您处理的每个模型,您都将创建@L_712_0@model.js文档,并将其添加到上面的models / index.js中。作为一个例子,我添加一个用户模型,但评论了它。

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

路线/ index.js

@H_197_11@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

@H_197_11@module.exports = function(app) { app.get('/blog',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数据,创建模型的实例,然后我发送查询到相关的模型。
>方法:对于不直接涉及模型的更复杂的逻辑。作为一个例子,我有一个算法/文件夹,我存储我在应用程序中使用的所有算法。

希望这样更加清晰。这个结构正在为我工​​作,因为我觉得很容易遵循。

大佬总结

以上是大佬教程为你收集整理的node.js – 基于Mongoose的应用程序架构全部内容,希望文章能够帮你解决node.js – 基于Mongoose的应用程序架构所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。