如何使用Sails.js定制路由中间件? (ExpressJS) [英] How to use custom route middleware with Sails.js? (ExpressJS)

查看:195
本文介绍了如何使用Sails.js定制路由中间件? (ExpressJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚打开了Node Framework Sails.js的新副本。它是建立在Express 3上。在/config/routes.js文件中有这个注释:

  / ** 
*(1)核心中间件
*
*app.use中包含的中间件首先运行,路由器前
* /


/ **
*(2)静态路由
*
*此对象将静态URL路由到处理函数 -
*在大多数情况下,这些函数是控制器内的操作。
*为了方便起见,您还可以将路由直接连接到视图或外部URL。
*
* /

module.exports.routes = {...

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

  //仅在ajax调用上运行API。 
module.exports.isAjax = function(req,res,next){
if(req.headers ['x-requested-with']){
//允许sails处理路由
return next();
} else {
//加载主模板文件
// ...
}
};

我的目的是使非Ajax GET请求都加载相同的模板文件,以便我的CanJS应用程序可以根据URL设置应用程序状态(所以我的javascript应用程序是正确的书签)。



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



我猜这是像

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

只有当我执行上述操作时,才会告诉我找不到快速模块。我已经验证了express是一个模块里面的Sails的node_modules。是否有另一种加载语法?我宁可不用安装第二份快速的副本。有没有办法访问原始的Sails / Express应用程序实例?

解决方案

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

 '*':'isAjax'


I've just unpacked a fresh copy of the Node framework Sails.js. It is built on Express 3. In the /config/routes.js file is this comment:

/**
 * (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 = { ...

In the same config folder I have created the file called 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
    // ...
  }
};

My intended purpose is to make non-Ajax GET requests all load the same template file so my CanJS application can set up the application state based on the URL (so my javascript application is properly bookmark-able).

I would like to run that script as middleware. Can somebody please show me how to use app.use() in this case to have the is_ajax.js script run before other routing?

I'm guessing it's something like

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

Only when I do the above, it tells me that it can't find the express module. I've verified that express is a module inside Sails' node_modules. Is there another syntax for loading it? I'd rather not have to install a second copy of express alongside sails. Is there a way to access the original Sails/Express app instance?

解决方案

You can use policies to achieve this. Save your isAjax function as isAjax.js under your api/policies folder, and change it to just use module.exports instead of module.exports.isAjax. Then in your config/policies.js file, you can specify which controllers/actions to apply the policy to--to run isAjax for every route, just do:

'*':'isAjax'

in that file.

这篇关于如何使用Sails.js定制路由中间件? (ExpressJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆