0

次の方法で、個々のルート内からExpress を取得appできることを知っています。

req.app

ただし、内部でモジュールの単一のインスタンスを開始する必要がありますroutes/index.js。つまり、次のようになります。

var myModule = require('my-module')(propertyOfApp) 

appから急行を取得するにはどうすればよいrouterですか?

4

1 に答える 1

0

それは本当にあなた自身の実装に依存しますが、コメントで私が提案したことはうまくいくはずです:

// index.js
module.exports = function(app) {
    // can use app here
    // somehow create your router and do the magic, configure it as you wish
    router.get('/path', function (req, res, next) {});

    return router;
}

// app.js
// actually call the function that is returned by require,
// and when executed, the function will return your configured router
app.use(require('./index')(app));

ps もちろん、これは単なるサンプルです。ルーターにパスと、必要なすべての種類のプロパティを設定できます。乾杯!:)

于 2015-09-16T17:09:55.553 に答える