【问题标题】:Requiring Node.js/Express.js Middleware has no Method '...'需要 Node.js/Express.js 中间件没有方法 '...'
【发布时间】:2013-12-16 20:25:46
【问题描述】:

我在需要 Express.js 中间件时遇到问题;我有app.'s中的代码:

...
var middlewares = require('./middlewares');
...
routes = require('./routes')(app, config, crypto, middlewares, models, oauth2);
...

这需要./middlewares/index.js:

/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/

'use strict';

var middlewares = function (app, config, models, oauth2) {
  var that = {};

  that.touch = require('./touch.js')(app, config, models, oauth2);

  return that;
};

module.exports = middlewares;

这反过来(上面是简化的,但也包含需要其他中间件)需要./middlewares/touch.js

/*jslint es5: true, indent: 2, node: true, nomen: true, maxlen: 80, vars: true*/

'use strict';

var touch = function (app, config, models, oauth2) {
  return function (req, res, next) {
    console.log('Touch');

    next();
  };
};

module.exports = touch;

并从./routes/index.js调用它:

/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/

'use strict';

var routes = function (app, config, crypto, middlewares, models, oauth2) {
  var that = {};

  app.get(
    '/',
    middlewares.touch(app, config, models, oauth2),
    function (req, res) {
      res.send('Hello world!');
    }
  );

  that.auth = require('./auth')(app, config, crypto, models, oauth2);

  return that;
};

module.exports = routes;

虽然当我这样做时,我得到了错误:

TypeError: Object function (app, config, models, oauth2) {
  var that = {};

  that.touch = require('./touch.js')(app, config, models, oauth2);

  return that;
} has no method 'touch'

当我需要来自app.js./middlewares/touch.js 时,例如:

...
var touch = require('./middlewares/touch.js');
...
routes = require('./routes')(app, config, crypto, touch, models, oauth2);
...

./routes/index.js 我这样称呼它:

/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/

'use strict';

var routes = function (app, config, crypto, touch, models, oauth2) {
  var that = {};

  app.get(
    '/',
    touch(app, config, models, oauth2),
    function (req, res) {
      res.send('Hello world!');
    }
  );

  that.auth = require('./auth')(app, config, crypto, models, oauth2);

  return that;
};

module.exports = routes;

我没有错误!一定是我通过./middlewares/index.js 引用它的方式,但我可以终生解决它吗?

任何想法堆栈溢出?

编辑:

好的,那么问题仍然存在;

  • 我想从内部require ./middlewares/index.js app.js
  • ./middlewares/index.js 需要我所有的中间件,例如./touch.js
  • 我只需要将middlewares 对象传递给其他模块即可访问其方法,例如middlewares 可以传递到我的 routes 模块中,我可以引用触摸中间件,例如 middlewares.touch()

非常简单;我希望 require 我所有的中间件并将它们传递到路由模块中,例如:

...
var middlewares = require('./middlewares');
...
routes = require('./routes')(app, middlewares);
...

并在如下路径中使用我的中间件:

/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/

'use strict';

var routes = function (app, middlewares) {
  var that = {};

  app.get(
    '/',
    middlewares.touch,
    function (req, res) {
      res.send('Hello world!');
    }
  );

  that.auth = require('./auth')(app);

  return that;
};

module.exports = routes;

我应该怎么做?看看这个问题的顶部,看看我现在是如何尝试(和失败)的,感谢大家的帮助!

此外,应该模块return 的东西,例如; routes 模块真的不需要返回任何东西,或者应该/应该吗?

【问题讨论】:

    标签: node.js methods express middleware


    【解决方案1】:

    首先关闭: 两个遮阳篷都是正确的

    中间件/index.js 在其导出中返回一个函数

    在 routes.js 中,您尝试使用该函数的成员 touch,它不存在

    其次:

    看起来你可以帮助我们构建你的应用程序,你已经非常彻底地完成了,但是这样做会使组件非常依赖于彼此/应用程序

    您注入依赖项(应用程序、配置、加密、中间件、模型、oauth2 等) 进入每个模块,而不是让它们拥有自己的模块

    在您的 app.js 中,您可能会执行类似的操作

    var OAuth2Lib = require('some/path/oauth2');
    var oauth2 = new OAuth2Lib(some.config);
    

    并重复所有其他 [crypto, cnfig, models,...] 完成后,您运行

    routes = require('./routes')(app, config, crypto, middlewares, models, oauth2);
    

    如果您将构建 oahth2 的构造代码放入一个新模块中,您可以在需要时要求它(放入 lib/oauth2.js):

    var OAuth2Lib = require('some/path/oauth2');
    var oauth2 = new OAuth2Lib(some.config);
    module.exports = oauth2;
    

    在里面使用:./middlewares/touch.js:

    var oauth2 = require('../lib/oauth2');
    var config = require('../config/config');
    var models = require('../lib/models');
    
    var touch = function (app) {
      return function (req, res, next) {
        console.log('Touch');
    
        next();
      };
    };
    
    module.exports = touch;
    

    现在只剩下应用程序依赖项(如果您需要的话) 注意中间件的顺序,尤其是像 cookieParse、bodyParser (express) 这样的东西应该在你自己的自定义中间件之前 app.use'd

    【讨论】:

      【解决方案2】:

      问题是middlewares 返回一个函数,该函数在执行时返回一个对象,而不是像您在./routes/index.js 中使用的那样仅返回一个对象。

      你可以做两件事中的一件。

      1) 更改 ./middlewares/index.js 以将 module.exports 设置为对象字面量。

      /*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/
      
      'use strict';
      
      var middlewares = {
      
        touch : require('./touch.js')(app, config, models, oauth2)
      
      };
      
      module.exports = middlewares;
      

      -- 或者--

      2) 在调用 touch 之前将 ./routes/index.js 更改为运行 middlewares

      /*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/
      
      'use strict';
      
      var routes = function (app, config, crypto, middlewares, models, oauth2) {
        var that = {};
      
        app.get(
          '/',
          middlewares().touch(app, config, models, oauth2),
          function (req, res) {
            res.send('Hello world!');
          }
        );
      
        that.auth = require('./auth')(app, config, crypto, models, oauth2);
      
        return that;
      };
      
      module.exports = routes;
      

      我偏爱选项 1,但这取决于您是否需要在构建返回对象之前在 ./middlewares/index.js 中执行某些操作。

      根据您拥有的代码,选项 1 似乎适合您的需求。

      【讨论】:

        【解决方案3】:

        每个函数都是一个对象。

        函数对象middlewares没有方法touch,这是真的。我不确定你想用这段代码实现什么,但touch 方法是函数返回,而不是函数本身的成员。

        就语法而言,这将起作用:middlwares().touch,或直接成员分配 middlwares.touch = ...,然后您就可以访问它了,尽管我非常怀疑这是否是您的想法。

        【讨论】:

        • 是的;但是./middlewares/touch.js 返回的函数在第一个示例中被分配给middlewares.touch,不是吗?尽管./middlewares/index.js虽然我更喜欢这种方式,因为我希望所有中间件都可以从一个对象中获得,例如middlewares.touchmiddlewares.otherFunc等。我认为这一定与./middlewares/index.js代理有关?
        • 谢谢,我已经添加到我的问题中了;您能否看一下并建议我应该如何执行此操作,不幸的是,我显然不明白您的第一个答案。能不能说的更全面点?感谢您迄今为止的帮助!
        猜你喜欢
        • 2015-01-20
        • 1970-01-01
        • 2011-12-23
        • 2020-04-14
        • 1970-01-01
        • 2020-07-19
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        相关资源
        最近更新 更多