# express-route-autocorrect

> Autocorrect 404 routes to the best match

Latest version **1.0.2** (published 2017-07-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-route-autocorrect
pnpm add express-route-autocorrect
yarn add express-route-autocorrect
bun add express-route-autocorrect
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2017-07-06 |
| First published | 2017-07-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Brahim Hadriche |
| Maintainers | ibicha |
| Keywords | express, middleware, autocorrect, route, redirect |

## Links

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

## Dependencies (1)

- [string-similarity](https://npm.io/package/string-similarity.md) ^1.2.0

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2017-07-06
- 1.0.1 — 2017-07-05
- 1.0.0 — 2017-07-05

## README

# express-route-autocorrect

[![npm version](https://badge.fury.io/js/express-route-autocorrect.svg)](https://badge.fury.io/js/express-route-autocorrect)

Middleware that autocorrects url routes to the closest match.
## Install

    $ npm install express-route-autocorrect --save
## Usage

`express-route-autocorrect` should be set as the last middleware, but right before the 404 not found error handler.
It will populate `req.urlBestMatch` with the best match so you can decide what to do with it.
The middleware takes an array of routes to compare then against the request.
```javascript
var routeAutocorrect = require('express-route-autocorrect');
//some other middlewares...

app.use('/', index);
app.use('/users', users);
app.use('/some/other/route', someOtherView);

app.use(routeAutocorrect([
    '/',
    '/users',
    '/some/other/route'
]));

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    // e.g. if req.url == '/user' then req.urlBestMatch will contain '/users'
    var err = new Error('Not Found. did you mean to go to ' + req.urlBestMatch + ' ?');
    err.status = 404;
    next(err);
});

// error handler view here...
```

You can also specify to redirect option, to automatically redirect to the `bestMatch`

```javascript
app.use(routeAutocorrect({
    routes: [
        '/',
        '/users',
        '/some/other/route'
    ],
    redirect: true
}));
```

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