Skip to content

snowron/route-typo-redirect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 && r.route.path !== "/")
          .map((r) => {
              return r.route.path
          });
  }

  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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published