我刚刚解开了Node框架Sails.js的全新副本。它是建立在快速3.在/config/routes.js文件是这样的评论:如何在Sails.js中使用自定义路由中间件? (ExpressJS)
/**
* (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文件相同的config文件夹。
// 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是Sails的node_modules中的一个模块。是否有另一种加载语法?我宁愿不必安装帆船的第二个快递副本。有没有办法访问原始的Sails/Express应用程序实例?
THX添加customMiddleware。(+1),但我不认为我会这样写 - thx为我们箭袋中的额外箭头。 – Cody