To use specific middleware in Express for all paths except a specific one with Node.js, we can check the path of the request in the middleware function.
For instance, we write
const checkUser = (req, res, next) => {
if (req.path == "/") {
return next();
}
next();
};
app.all("*", checkUser);
to call app.all
to add the checkUser
middleware for all routes.
In checkUser
, we check the path of the request with req.path
.
If it’s '/'
, we call the next middleware with next
and return to end the execution of checkUser
.