Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJS Applying middleware only to routes in router

I have app where I have public routes and authorized routes. Public routes should go through auth as well, but if auth fails, it doesn't matter.

So I have two routers:

var publicRoutes = express.Router();
var secretRoutes = express.Router();

publicRoutes
    .use(auth)
    .use(ignoreAuthError);

publicRoutes.get('/public', function(req, res){
    res.status(200).send({message: "public"});
}); 

secretRoutes
    .use(auth)
    .use(handleAuthError);

secretRoutes.get('/secret', function(req, res){
    res.status(200).send({message: "secret"});
}); 

...

app.use(publicRoutes);
app.use(secretRoutes);

Now everything works fine, but if I change the order of app.use public routes throw auth error. Also I cannot get any 404, 500 etc errors, because they all go through auth errors.

So obviously what is happening is that Router.use() is being applied to all routes with the same root - in this case "/"

Therefore I think if I would use just auth middleware on all routes and then add other middlewares directly to routes it should work fine. But it kind of brakes the point of having multiple Routers for me.

I would expect that if I use Router.use() the middleware will apply only if that particular router matches any routes it has set up, instead of changing behavior of other router.

Do I understand this correctly? Is there any way to handle this without actually having to add middleware to every single route?

like image 970
Tomas Avatar asked Feb 18 '16 18:02

Tomas


People also ask

How do I use middleware on route Express?

Use third-party middleware to add functionality to Express apps. Install the Node. js module for the required functionality, then load it in your app at the application level or at the router level. The following example illustrates installing and loading the cookie-parsing middleware function cookie-parser .

Are routes middleware?

They are not middleware functions by definition. If such function is used on routing methods then they are only handler functions. We use such a handler function which is not a middleware when it is the only one callback function.

What a middleware can do in ExpressJS?

An Express-based application is a series of middleware function calls. Advantages of using middleware: Middleware can process request objects multiple times before the server works for that request. Middleware can be used to add logging and authentication functionality.

Can we use multiple middleware in node?

We can use more than one middleware on an Express app instance, which means that we can use more than one middleware inside app. use() or app. METHOD() . We use a comma ( , ) to separate them.

How do I make a route have no authentication in expressjs?

In Expressjs, every middleware you add, gets added to the middleware stack, i.e. FIFO. Thus, if you have certain routes, which you'd like to have no authentication, you can simply keep their middlewares above others. Hope this helps!

What are the different types of middleware in express?

There are two main types of middleware, i.e. Global middleware (This is a middleware which can be accessed by all routes in the application hence its name “Global”) and specific middleware (This is a middleware which applies to just a specific route) Using a global middleware in an express application makes use of the syntax app.use (middleware);

What is router class in Express Express?

express.Router Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.

How to define routes in an express application?

Now, the syntax for defining routes in an express application includes where method refers to the HTTP method/verb (get,post,put etc.) to apply, path refers to the route or URL at which the request will apply, also callback is the function or middleware (explained down below) which will run when a request an http method hit to path.


1 Answers

Had the same issue, solved thanks to @Explosion Pills comment.

Bad:

app.use(secretRoutes); // router.use calls won't be scoped to "/secret" 
app.use(publicRoutes); // public routes will be impacted

Good:

app.use("/secret", secretRoutes); // router.use calls will be scoped to "/secret" 
app.use("/public", publicRoutes); // public routes won't be impacted
like image 158
Pleymor Avatar answered Oct 20 '22 17:10

Pleymor