Conditionally skip a middleware when a condition is met.当满足条件时,有条件地跳过中间件。
npm i express-unless --save
With existing middlewares:对于现有中间件:
var { unless } = require("express-unless");
var static = express.static(__dirname + "/public");
static.unless = unless;
app.use(static.unless({ method: "OPTIONS" }));
If you are authoring a middleware you can support unless as follow:如果您正在创作中间件,您可以支持,除非如下所示:
var { unless } = require("express-unless");
module.exports = function (middlewareOptions) {
var mymid = function (req, res, next) {};
mymid.unless = unless;
return mymid;
};
method
path
ext
custom
req
and returns true
/ false
. req
并返回true
/false
的函数。true
,则中间件将不会运行。useOriginalUrl
true
or false
, default is true
. true
或false
,默认值为true
。path
will match against req.url
instead of req.originalUrl
. false
,则path
将与req.url
而不是req.originalUrl
匹配。req.url
and req.originalUrl
.req.url
和req.originalUrl
之间的区别。Require authentication for every request unless the path is index.html.要求对每个请求进行身份验证,除非路径是index.html。
app.use(
requiresAuth.unless({
path: ["/index.html", { url: "/", methods: ["GET", "PUT"] }],
})
);
Avoid a fstat for request to routes doesnt end with a given extension.避免fstat,因为路由请求不会以给定的扩展名结束。
app.use(
static.unless(function (req) {
var ext = url.parse(req.originalUrl).pathname.substr(-4);
return !~[".jpg", ".html", ".css", ".js"].indexOf(ext);
})
);
MIT 2014 - Jose Romaniello