0

I would like to use a middleware for checking users credentials only for some routes (those that start with /user/), but to my surprise server.use does not take a route as first argument and with restify-namespace server.use effect is still global.

Is there other way better than passing my auth middleware to all routes alongside the controller?

fortran
  • 74,053
  • 25
  • 135
  • 175

2 Answers2

0

I think I'm going to just use server.use and inside the middleware make the following route check:

if (req.url.indexOf('/user/') !== 0) {
    return next();
}
fortran
  • 74,053
  • 25
  • 135
  • 175
0

Unfortunately restify doesn't seem to be like express, which support the * operator. Hence, What I would suggest is grouping the routes that you desire together and apply a .use before them.

That is:

server.get('/test', function(req, res, next) {
  // no magic here. server.use hasn't been called yet.
});

server.use(function(req, res, next) {
  // do your magic here
  if(some condition) {
    // magic worked!
    next(); // call to move on to the next middleware.
  } else {
    // crap magic failed return error perhaps?
    next(new Error('some error')); // to let the error handler handle it. 
  } 
});

server.get('/admin/', function(req, res, next) {
  // magic has to be performed prior to getting here!
});

server.get('/admin/users', function(req, res, next) {
  // magic has to be performed prior to getting here!
});

However, I would personally advocate the use of express, but choose whatever fits your need.

lwang135
  • 576
  • 1
  • 3
  • 13