Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can chain handlers in a route. Your handler definition should be like</p> <pre><code>routehandler(req, res, next){ //send response directly //or call next to call the next handler } </code></pre> <p>You can then put multiple handlers in the order you want:</p> <pre><code>app.get('/server', ensureAuthenticated, housekeeping, routes.server.get) </code></pre> <p>Here <code>ensureAuthenticated</code> and <code>housekeeping</code> dont send the response just call <code>next()</code>. The last one returns the resulting page.</p> <p>Please see you would want a different handler than what you use for login. Login page would do authentication, rest pages should just check if the user is authenticated or not. The difference would be clear if you are using sessions.</p> <hr> <p>The args for the route handlers are the same as that of middlewares. Those are :</p> <ol> <li>(err, req, res, next) Error handlers</li> <li>(req, res, next) Non-error handlers</li> </ol> <p>A trivial variation of above is that <code>next</code> can be left out if it is the end function you want in callback chain. You cannot have other args apart from these. You can see the how they are called <a href="https://github.com/visionmedia/express/blob/master/lib/router/index.js" rel="nofollow">here</a> (line 154).</p> <p>A route consists of method, route-match and callback array. Like the middlewares the callback chain is executed sequentially for a specific route until response is returned or error is thrown. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload