Conditionally skip a middleware when a condition is met.当满足条件时,有条件地跳过中间件。
npm i express-unless --saveWith 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;
};methodpathextcustomreq and returns true / false. req并返回true/false的函数。true,则中间件将不会运行。useOriginalUrltrue 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