从中间件访问快速应用程序



我有一个中间件,我这样称呼它:

app.use(example({optiona:'abc'}));

我想从中间件功能访问该应用程序,并执行另一个 app.use,如下所示:

module.exports = function (options){
    app.use(...);
    return function(req, res, next)
        next();
}

我知道将应用程序传递给导出的选项,但我想在没有传递它或将其设置为全局选项的情况下执行此操作。

您可以尝试将高速路由器作为一种选择(阅读所有相关信息)。

从本质上讲,您可以保持您的第一件作品相同:

app.use(example({optiona:'abc'}));

然后,您可以在函数中执行以下操作:

var express = require("express");
var router = express.Router();
module.exports = function(options) {
    // You can declare middleware specific to this router
    router.use(...);
    // You can declare routes specific to this router
    router.get("/foo", ...);
    router.all("/bar", ...);
    // Then just return the router at the end
    return router;
}

路由器允许您相应地使用自己的路由/中间件设置"子应用程序"。

相关内容

最新更新