I want to check the authorization of the users of my web app when they entered the url. But when I used an individually middleware to check the authorization, it's useless for the already existing routes, such as:
function authChecker(req, res, next) {
if (req.session.auth) {
next();
} else {
res.redirect("/auth");
}
}
app.use(authChecker);
app.get("/", routes.index);
app.get("/foo/bar", routes.foobar);
The authChecker is unabled to check the authority of the users who entered the two urls. It only works for the unspecified urls.
And I saw a method that I can put the authChecker between the route and the route handler, such as:
app.get("/", authChecker, routes.index);
But how can I achieve it in a simple way rather than putting the authChecker in every route?