简体   繁体   English

如何构建我的nodejs代码? 胖模型/瘦控制器

[英]How to structure my nodejs code? fat model / skinny controller

My code works, but I understand it's best to have fat models and skinny controllers. 我的代码有效,但是我知道最好有胖模型和瘦控制器。

However, I am using 3 different models and have unfortunately made my controller fat. 但是,我使用的是3种不同的模型,不幸的是我的控制器变胖了。 What is the best way to organize this code (with fat model / skinny controller concept)? 组织此代码的最佳方法是什么(使用胖模型/瘦控制器概念)? I've been reading on code structure, but I'm a bit unsure of best practices. 我一直在阅读代码结构,但是我不确定最佳实践。

Controller: 控制器:

var Product = require('../models/product');
var Collection = require('../models/collection');
var Vote = require('../models/vote');

exports.topSearch = function(req, res) {
  console.log(req.body, "search product")

  Product.search({
      query_string: {
        query: req.body.search
      }
    },req.body.searchObject,
    function(err, results) {
      if (err) console.log('ERR', err);
      if (results) {
        var data = results.hits.hits;

        Vote.find({
          user: req.user._id
        }, function(err, votes) {
          if (!err) {
            for (var i = 0; i < votes.length; i++) {
              for (var j = 0; j < data.length; j++) {
                if (data[j]['_id'] == votes[i]['product']) {
                  data[j]['voteId'] = votes[i]['_id'];
                  data[j]['userVote'] = votes[i]['vote'];
                }
              }
            }
          }

        Collection.find({
          user: req.user._id
        }, function(err, collections) {
          if (!err) {
            for (var i = 0; i < collections.length; i++) {
              for (var j = 0; j < data.length; j++) {
                if (data[j]['_id'] == collections[i]['product']) {
                  console.log('match')
                  data[j]['collected'] = true;
                  data[j]['collectId'] = collections[i]['_id'];
                  data[j]['favorite'] = collections[i]['favorite'];
                } else if (data[j]['_id'] !== collections[i]['product'] && data[j]['collected'] !== true) {
                  data[j]['collected'] = false;
                }
              }
            }
            res.send(data);
          }
        });
      });

      } else {
        res.send({
          errmsg: 'results not defined'
        })
      }
    });
};

I then call this in my route: 然后,我在路线中称其为:

  app.post('/products-search', users.ensureAuthenticated, products.topSearch);

You can create a separate file under models or functions, where you can do all your model based processing related to "topSearch" and then simply call a single method inside of your controller. 您可以在模型或函数下创建一个单独的文件,您可以在其中进行与“ topSearch”相关的所有基于模型的处理,然后只需在控制器内部调用单个方法即可。

Use something like async.js to better handle callbacks and flow. 使用async.js之类的东西可以更好地处理回调和流程。

I personally follow the folder structure defined in this repo. 我个人遵循此仓库中定义的文件夹结构。

better-node-express-app-structure 更好的节点快车-APP-结构

And sometimes I add subfolders into models and controllers depending on the complexity. 有时,我会根据复杂性在模型和控制器中添加子文件夹。

Frequently used select queries can be directly exported as independent functions to facilitate reusability. 可以将常用的选择查询直接导出为独立功能,以促进可重用性。

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

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