route-typo-redirect

1.0.4 • Public • Published

route-typo-redirect

Make your users happy with catch typos with middleware and redirect to right one.

Works with Express

Install

  npm i route-typo-redirect

Usage

Middleware needs your routes with array of strings. It gets the request path and analyzes it with rules and redirects.

const typoRedirect = require('route-typo-redirect')

app.use(typoRedirect({
    routes: ["/chat", "/about/:id", "/home"]
}))

app.use(typoRedirect({
    routes: ["/chat", "/about/:id", "/home"],
    blacklist: ["/user"],
}))

app.use(typoRedirect({
    routes: ["/chat", "/about/:id", "/home"],
    blacklist: ["/user"],
    levenThreshold: 2,
    fuzzyThreshold: 4,
}))

Also there is a cool snippet to get all routes from Express

  function availableRoutes() {
    return app._router.stack
      .filter((r) => r.route)
      .map((r) => {
        return r.route.path !== "/" ? r.route.path : null;
      });
  }

  app.use(typoRedirect({
    routes: availableRoutes(),
    blacklist: ["/user"],
    levenThreshold: 2,
    fuzzyThreshold: 4,
}))

Demo

Example

Tests

➜ curl "http://localhost:3000/abouc/"           
Found. Redirecting to /about/%                                                                                                                                                                             

➜ curl "http://localhost:3000/abouct/"
Found. Redirecting to /about/%    

➜ curl "http://localhost:3000/chay/123"
Found. Redirecting to /chat/123%     

Tests

  npm run test

Options

Parameter Type Desc
blacklist array<string> these routes not redirect
routes array<string> whitelist routes
levenThreshold number the levenstein algorithm's threshold value
fuzzyThreshold number fuzzy comparasion algorithm's threshold value

Lisans

MIT

Dependents (0)

Package Sidebar

Install

npm i route-typo-redirect

Weekly Downloads

4

Version

1.0.4

License

ISC

Unpacked Size

7.54 kB

Total Files

5

Last publish

Collaborators

  • snowron