Make your users happy with catch typos with middleware and redirect to right one.
Works with Express
npm i route-typo-redirect
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,
}))
➜ 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%
npm run test
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 |