【问题标题】:How to use custom route middleware with Sails.js? (ExpressJS)如何在 Sails.js 中使用自定义路由中间件? (ExpressJS)
【发布时间】:2013-09-04 02:59:03
【问题描述】:

我刚刚解压了 Node 框架 Sails.js 的新副本。它建立在 Express 3 之上。在 /config/routes.js 文件中是这样的注释:

/**
 * (1) Core middleware
 *
 * Middleware included with `app.use` is run first, before the router
 */


/**
 * (2) Static routes
 *
 * This object routes static URLs to handler functions--
 * In most cases, these functions are actions inside of your controllers.
 * For convenience, you can also connect routes directly to views or external URLs.
 *
 */

module.exports.routes = { ...

在同一个配置文件夹中,我创建了名为 is_ajax.js 的文件。

// Only run through API on ajax calls.
module.exports.isAjax = function(req, res, next){
  if (req.headers['x-requested-with']) {
    // Allow sails to process routing
    return next();
  } else {
    // Load main template file
    // ...
  }
};

我的预期目的是让所有非 Ajax GET 请求都加载相同的模板文件,这样我的 CanJS 应用程序就可以根据 URL 设置应用程序状态(这样我的 javascript 应用程序就可以正确添加书签了)。

我想将该脚本作为中间件运行。 有人可以告诉我如何在这种情况下使用 app.use() 让 is_ajax.js 脚本在其他路由之前运行吗?

我猜是这样的

var express = require('express');
var app = express();
app.use( require('./is_ajax') );

只有当我执行上述操作时,它才告诉我它找不到 express 模块。我已经验证 express 是 Sails 的 node_modules 中的一个模块。是否有另一种语法来加载它?我宁愿不必在风帆旁边安装第二个 express 副本。有没有办法访问原始 Sails/Express 应用程序实例?

【问题讨论】:

    标签: node.js express sails.js


    【解决方案1】:

    您可以使用policies 来实现此目的。将 isAjax 函数保存为 api/policies 文件夹下的 isAjax.js,并将其更改为仅使用 module.exports 而不是 module.exports.isAjax。然后在您的 config/policies.js 文件中,您可以指定将策略应用到哪些控制器/操作——为每个路由运行 isAjax,只需:

    '*':'isAjax'
    

    在那个文件中。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,即弄清楚如何使用中间件。 它们基本上是在config/policies.js中定义的。
      因此,如果您想像旧样式一样使用 middlewares(又名 policies),您可以执行以下操作(这可能不是最好的方式):

      // config/policies.js
      '*': [ 
        express.logger(),
        function(req, res, next) {
          // do whatever you want to
          // and then call next()
          next();
        }
      ]
      

      然而,真正的sailjs方法是将所有此类策略放在api/policies/文件夹中

      【讨论】:

      • 谢谢。 (+1) 但我不认为我会这样写——谢谢我们箭袋中的额外箭头。
      【解决方案3】:

      要添加express的压缩中间件,我找到这个线程和

      sails-middleware-example-issue 很有用。

      1. 本地安装快递:npm install express
      2. 加载快递:var exp = require('express')
      3. $app_dir/config/local.js中添加自定义中间件
      express: {
          customMiddleware: function (app) {
            console.log("config of Middleware is called");
            app.use(exp.logger());
            app.use(exp.compress());
            app.use(function (req, res, next) {
              console.log("installed customMiddleware is used");
              next();
            })
          }
        }
      

      【讨论】:

      • 只是一个注释——你不需要添加 Express 作为依赖,只是为了在 Sails 中做自定义中间件!在您的示例中,您在自定义函数中使用了其他 Express 中间件,这很好,但如果您不这样做,那么您可以跳过步骤 1 和 2。
      • "express:" 已弃用(在sails 0.12.1中)。使用“http:”
      猜你喜欢
      • 2017-05-30
      • 1970-01-01
      • 2021-09-06
      • 2023-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多