【问题标题】:Sails.js - How do I reuse (most) of this code across multiple controller actions?Sails.js - 如何在多个控制器操作中重用(大部分)此代码?
【发布时间】:2014-12-16 01:32:18
【问题描述】:

我正在使用特定的 npm 模块获取提要,我想做的是使用相同的代码创建多个操作,尽管我不想重复整个代码。这是我的控制器:

module.exports = {

    buzzy: function (req, res) {
        var FeedParser = require('feedparser'),
            request = require('request');

        var req = request('http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml'),
            feedparser = new FeedParser();

        req.on('error', function (error) {
            // handle any request errors
        });
        req.on('response', function (res) {
            var stream = this;

            if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));

            stream.pipe(feedparser);
        });


        feedparser.on('error', function (error) {
            // always handle errors
        });
        feedparser.on('readable', function () {
            // This is where the action is!
            var stream = this,
                meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
                ,
                item;

            while (item = stream.read()) {
                var newData = item;                        
                   Buzzfeed.create({'title': newData.title, 'url': newData.link, 'source': 'nytimesTech', 'category': 'tech'}, function (err, newTitles) {
                        });                       

            }
        });

    }
};

与“嗡嗡”控制器动作非常相似,我想创建多个动作 - 以下是每个控制器中唯一的行

var req = request('http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml'),

Buzzfeed.create({'title': newData.title, 'url': newData.link, 'source': 'nytimesTech', 'category': 'tech'}, function (err, newTitles) {
                            });  

很好奇,实现这一点的最佳方法是什么,所以我不再重复了?

【问题讨论】:

    标签: node.js model-view-controller sails.js


    【解决方案1】:

    您可以使用服务。如果您有在代码中的多个位置使用的函数,您可以使用它们。

    在此处查看文档:Services

    【讨论】:

      【解决方案2】:

      /api/services 文件夹中,创建一个文件,将其命名为BuzzyAPI.js(比如说),并添加以下代码:

      var FeedParser = require('feedparser'),
      request = require('request');
      
      module.exports = {
      
        buzzy: function (reqUrl) {
      
          var req = request(reqUrl),
          feedparser = new FeedParser();
      
          req.on('error', function (error) {
      
          });
          req.on('response', function (res) {
            var stream = this;
            if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
            stream.pipe(feedparser);
          });
          feedparser.on('error', function (error) {
      
          });
          feedparser.on('readable', function () {
            var stream = this,
            meta = this.meta,
            item;
            while (item = stream.read()) {
              var newData = item;                        
              Buzzfeed.create({'title': newData.title, 'url': newData.link, 'source': 'nytimesTech', 'category': 'tech'}, function (err, newTitles) {
      
              });
            }
          });    
        }
      };
      

      现在您可以从任何controller 调用Service 并提供所需的URL 使用:

      BuzzyAPI.buzzy(url);
      

      希望这会有所帮助。

      【讨论】:

      • 谢谢 :-) 这解决了它,所以本质上是通过创建一个服务控制器作为一个对象,其中一个对象具有一个带参数的函数,然后在控制器操作中传递适当的参数就可以了:D
      • 如果这解决了问题,那么请考虑通过接受这个答案来解决这个问题:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多