Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Need for something like next() with .before #67

Open
arxpoetica opened this Issue · 1 comment

2 participants

@arxpoetica

I have a case where it's taking me a little while to do something in the .before() request handler. Express.js's method for handling this kind of behavior is to either send a response or to call next(); I realize this might be a major feature request, but .before() is misleading because it doesn't actually wait to run any routes. It just does stuff sequentially, without any pause.

My example:

this.before(function (req) {
    // send a request to the server to see if user is authenticated
    // if user is authenticated, allow the route to continue (next())
    // if user is not authenticated, reroute them
});

The problem is, since it takes a split second to ping the server for this information (in my case, there's a reason to not just use local storage), IT ACTUALLY RENDERS THE ADMIN PAGE before rerouting. It's only a split second, but this is unfortunate. :(

Basically, 1) is there a next() concept in Davis.js, and 2) if not, why, and 3) can there be something like next() added?

@olivernn
Owner

Davis actually uses a very similar approach to express with route middleware. These are currently only at an individual route level but should acheive exactly what you are looking for.

var isAdmin = function (req, next) {
    checkForAdminRole(function (success) {
        if (success) {
            next(req)
        } else {
            // do whatever
        }
    })
}

this.get('/admin', isAdmin, function (req) {
    // admin related stuff in here
})

This is actually the direction I would like to get before filters to go, but this is a way of yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.