To conditionally skip a middleware when a condition is met, we can use the express-unless
package.
In this article, we’ll take a look at how to use it.
Installation
We can install it by running:
npm i express-unless --save
Usage
After installing it, we can assign the unless
function exposed by the express-unless
and assign it to the unless
property of our middleware functions.
For example, we can write:
const express = require('express');
const unless = require('express-unless');
const static = express.static(__dirname + '/public');
static.unless = unless;const app = express();
app.use(static.unless({ method: 'OPTIONS' }));
app.listen(3000, () => console.log('server started'));
In the code above, we set the unless
property of static
to the unless
function of the express-unless
package.
Then we write:
app.use(static.unless({ method: 'OPTIONS' }));
to exclude the static
route from running on OPTIONS requests.
If we’re writing our own middleware, we can assign the unless
property of our middleware function as follows:
const express = require('express');
const unless = require('express-unless');
const static = express.static(__dirname + '/public');
const logHostname = (req, res, next) =>{
console.log(req.hostname);
next();
}
logHostname.unless = unless;const app = express();
app.use(logHostname.unless({ method: 'OPTIONS' }));
app.listen(3000, () => console.log('server started'));
In the code above, we defined the logHostman
middleware to log the hostname. Then we set the unless
property of the function to the unless
function exposed in the express-unless
package.
Options
In the object that we pass into unless
, we can pass in the following options:
method
— this can be a string or an array of strings. If the request method matches then the middleware won’t run.path
— this can be a string, regex or an array of string or regex. It can also be an array of object which is URL and method key-value pairs. If the request path or path and method match, then the middleware won’t run.ext
— this is a string or array of strings. If the path ends with those extensions then a middleware won’t run.custom
— a function that acceptsreq
and returns a boolean. If the function returnstrue
for the given request, then the middleware won’t run.useOriginalUrl
— this should be a boolean. Defaults totrue
. If it’sfalse
, thepath
will match againstreq.url
instead ofreq.originalUrl
.
Advanced Examples
We can exclude calling the express.static
middleware when we have the jpg
, html
, css
or .js
extensions as follows:
const express = require('express');
const unless = require('express-unless');
const url = require('url');
const static = express.static(__dirname + '/public');
static.unless = unless;
const app = express();
app.use(static.unless((req) => {
const ext = url.parse(req.originalUrl).pathname.substr(-4);
return !['.jpg', '.html', '.css', '.js'].includes(ext);
}));
app.listen(3000, () => console.log('server started'));
In the code above, we check the extensions of the req.originalUrl
property to see if they the request have the extensions listed in the array and skip the static
middleware if they’re there.
We can also mix paths and request methods together as follows:
const express = require('express');
const unless = require('express-unless');
const static = express.static(__dirname + '/public');
const logHostname = (req, res, next) => {
console.log(req.hostname);
next();
}
logHostname.unless = unless;
const app = express();
app.use(logHostname.unless({
path: [
'/index.html',
{ url: '/', methods: ['OPTIONS', 'PUT'] }
]
}));
app.listen(3000, () => console.log('server started'));
Then we stop the logHostname
middleware from running when we go to index.html
or /
with the PUT or OPTION request methods.
Conclusion
We can stop middleware from running on certain routes or request methods by using the express-unless
package.
To use it, we just set the unless
function exposed from the package and set it to the unless
property of our middleware function.
Then we can exclude routes by using various conditions by checking the URL and/or request method.