简体   繁体   English

如何使用node.js / Express 3和Mongoose(MongoDB)来组织代码/逻辑

[英]How to organize code / logic with node.js / Express 3, and Mongoose (MongoDB)

I see lots of examples where node.js / express router code is organized like this: 我看到许多示例,其中将node.js / express路由器代码组织如下:

// server.js // server.js

var cats = require('cats');
app.get('/cats', cats.findAll);

// routes/cats.js // route / cats.js

exports.findAll = function(req, res) {
    // Lookup all the cats in Mongoose CatModel.
};

I'm curious if it would be okay to put the logic to create, read, update and delete cats in the mongoose CatModel as methods? 我很好奇是否可以将在猫鼬CatModel创建,读取,更新和删除猫的CatModel作为方法吗? So you could do something like cat.findAll(); 因此,您可以执行类似cat.findAll(); The model might look something like this: 该模型可能看起来像这样:

var Cat = new Schema({
   name: {
     type: String,
     required: true
   }
});

Cat.methods.findAll = function(callback) {
   // find all cats.
   callback(results);
}

Then you could use this in your router: 然后,您可以在路由器中使用它:

app.get('/cats', cats.findAll);

If if further logic / abstraction is needed (to process the results) then one could do it in routes/cats.js . 如果需要进一步的逻辑/抽象(以处理结果),则可以在routes/cats.js

Thanks in Advance. 提前致谢。

Obviously your architecture is completely up to you. 显然,您的体系结构完全取决于您。 I've found that separating my routes (which handle business logic) and models (which interact with the db) is necessary and very easy. 我发现将我的路由(处理业务逻辑)和模型(与数据库交互)分开是必要且非常容易的。

So I would usually have something like 所以我通常会喜欢

app.js app.js

var cats = require ('./routes/cats');
app.get('/api/cats', cats.getCats);

routes/cats.js 路线/cats.js

var Cats = require ('../lib/Cats');

exports.getCats = function (req, res, next) {
  Cat.get (req.query, function (err, cats) {
  if (err) return next (err);
  return res.send ({
    status: "200",
    responseType: "array",
    response: cats
    });
  });
};

lib/Cat.js lib / Cat.js

var catSchema = new Schema({
   name: {
     type: String,
     required: true
   }
});

var Cat = mongoose.model ('Cat', catSchema);

module.exports = Cat;

Cat.get = function (params, cb) {
  var query = Cat.find (params);
  query.exec (function (err, cats) {
    if (err) return cb (err);
    cb (undefined, cats);
  });
};

So this example doesn't exactly show an advantage, but if you had an addCat route, then the route could use a "getCatById" function call, verify the cat doesn't exist, and add it. 因此,此示例并未完全显示出优势,但是,如果您有addCat路由,则该路由可以使用“ getCatById”函数调用,验证是否存在该猫并添加它。 It also helps with some nesting. 它还有助于进行一些嵌套。 The routes could also be used for sanitizing the objects before sending them off, and might also send resources and information used in UI that isn't necessarily coupled with mongoose. 路由还可以用于在发送对象之前对其进行清理,并且还可以发送UI中使用的不一定与猫鼬耦合的资源和信息。 It also allows interactions with the database to be reusable in multiple routes. 它还允许与数据库的交互可在多个路由中重用。

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

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