title: Express: call a middleware from another middleware
body: <p>I'm new to <strong>Express 4</strong> and i'm wondering something about how to implement this thing: I'm using jwt to authenticate the consumer of my API, to do that i have a pretty simple middleware to check the validity of the jwt token:</p>
  
  <pre><code>var requireValidToken = function(req, res, next) {
    var token = req.body.token || req.query.token || req.headers['x-access-token'];
    if (token) {
      try {
        var decoded = jwt.verify(token, req.app.get('superSecret'));
      } catch(err) {
        return res.json({ success: false, message: 'Failed to authenticate token.' });  
      }
      req.user = decoded.user;
      next();
    } else {
      return res.status(403).send({ 
          success: false, 
          message: 'No token provided.' 
      });
    }
  };
  </code></pre>
  
  <p>This is working pretty well, but now, i want to extend this to be able to check the role of the user:</p>
  
  <pre><code> router.get('/anAdminRoute', requireRole('ROLE_ADMIN'), function (req, res, next) {
        // some code...
   });
  </code></pre>
  
  <p>So i added a middleware for this:</p>
  
  <pre><code>var requireRole = function(role) {
    return function(req, res, next){
      // Dummy tests...
      if(req.user.role == roles.admin || req.user.role == role){
        next();
      } else {
        return res.status(403)({
          success: false,
          message: "Token valid, but you don't have the right permission to access this resource :)"
        });
      }
    }
  }
  </code></pre>
  
  <p>But as this requireRole() function while obviously checks for a valid jwt token, i'm wondering how can i call my requireValidToken middleware within this function, and so not having to explicitly call it for each route i want to protect.</p>
  
  <p>An easy solution would have been not to use <strong>requireValidToken</strong> as a middleware but i still want to be able to use it to protect certain routes</p>
  
  <p><strong>Edit: Solution</strong></p>
  
  <p>Chaining middlewares is a simple as that:</p>
  
  <pre><code>var middleware2 = function(param) {
    return function(req, res, next){
      middleware1(req, res, function(){
        // middleware2 code
      });
    }
  }
  </code></pre>
  
  <p>If anybody interested, my final working solution to validate a user role:</p>
  
  <pre><code>var jwt = require('jsonwebtoken'),
    roles = require('../models/user').roles; 
  
  // authentication middleware
  // check if the given jwt token is valid
  var requireValidToken = function(req, res, next) {
    var token = req.body.token || req.query.token || req.headers['x-access-token'];
    if (token) {
      try {
        var decoded = jwt.verify(token, req.app.get('superSecret'));
      } catch(err) {
        return res.json({ success: false, message: 'Failed to authenticate token.' });  
      }
      req.user = decoded.user;
      next();
    } else {
      return res.status(403).send({ 
          success: false, 
          message: 'No token provided.' 
      });
    }
  };
  
  var requireRole = function(role) {
    return function(req, res, next){
      requireValidToken(req, res, function(){
        if(req.user.role == roles.admin || req.user.role == role){
          next();
        } else {
          return res.status(403).send({
            success: false,
            message: "Token valid, but you don't have the right permission to access this resource :)"
          });
        }
      });
    }
  }
  
  module.exports = {
    requireValidToken: requireValidToken,
    requireRole: requireRole
  }
  </code></pre>