1.0.4 • Published 2 years ago

route-typo-redirect v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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

ParameterTypeDesc
blacklistarray<string>these routes not redirect
routesarray<string>whitelist routes
levenThresholdnumberthe levenstein algorithm's threshold value
fuzzyThresholdnumberfuzzy comparasion algorithm's threshold value

Lisans

MIT