# express-unless

> Conditionally add a middleware to express with some common patterns.

Latest version **2.1.3** (published 2022-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-unless
pnpm add express-unless
yarn add express-unless
bun add express-unless
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.3 |
| Published | 2022-10-24 |
| First published | 2014-07-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 180 |
| Author | José F. Romaniello |
| Maintainers | jfromaniello |

## Links

- npm: https://www.npmjs.com/package/express-unless
- Repository: https://github.com/jfromaniello/express-unless
- Homepage: https://github.com/jfromaniello/express-unless#readme
- Issues: https://github.com/jfromaniello/express-unless/issues
- npm.io page: https://npm.io/package/express-unless

## Recent versions

- 2.1.3 (latest) — 2022-10-24
- 2.1.2 — 2022-09-28
- 2.1.1 — 2022-07-14
- 2.1.0 — 2022-06-01
- 2.0.2 — 2022-05-31
- 2.0.1 — 2022-05-31
- 2.0.0 — 2022-05-31
- 1.0.0 — 2021-08-03
- 0.5.0 — 2018-01-23
- 0.4.0 — 2018-01-23
- 0.3.1 — 2017-08-02
- 0.3.0 — 2015-08-27
- 0.1.1 — 2015-07-15
- 0.1.0 — 2015-01-19
- 0.0.0 — 2014-07-16

## README

Conditionally skip a middleware when a condition is met.

## Install

    npm i express-unless --save

## Usage

With existing middlewares:

```javascript
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:

```javascript
var { unless } = require("express-unless");

module.exports = function (middlewareOptions) {
  var mymid = function (req, res, next) {};

  mymid.unless = unless;

  return mymid;
};
```

## Current options

- `method` it could be an string or an array of strings. If the request method match the middleware will not run.
- `path` it could be an string, a regexp or an array of any of those. It also could be an array of object which is url and methods key-pairs. If the request path or path and method match, the middleware will not run. Check [Examples](#examples) for usage.
- `ext` it could be an string or an array of strings. If the request path ends with one of these extensions the middleware will not run.
- `custom` it must be a function that accepts `req` and returns `true` / `false`. If the function returns true for the given request, the middleware will not run.
- `useOriginalUrl` it should be `true` or `false`, default is `true`. if false, `path` will match against `req.url` instead of `req.originalUrl`. Please refer to [Express API](http://expressjs.com/4x/api.html#request) for the difference between `req.url` and `req.originalUrl`.

## Examples

Require authentication for every request unless the path is index.html.

```javascript
app.use(
  requiresAuth.unless({
    path: ["/index.html", { url: "/", methods: ["GET", "PUT"] }],
  })
);
```

Avoid a fstat for request to routes doesnt end with a given extension.

```javascript
app.use(
  static.unless(function (req) {
    var ext = url.parse(req.originalUrl).pathname.substr(-4);
    return !~[".jpg", ".html", ".css", ".js"].indexOf(ext);
  })
);
```

## License

MIT 2014 - Jose Romaniello

---
_Source: https://npm.io/package/express-unless · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
