title: How to pass JsonWebToken x-access-token through angular js
body: <p>I created a node express RESTful API with jsonwebtoken as authentication method. But unable to pass the x-access-token as headers using angular js.</p>
  
  <p>my JWT token authentication script is,</p>
  
  <pre><code>apps.post('/authenticate', function(req, res) {
  
      // find the item
      Item.findOne({
          name: req.body.name
      }, function(err, item) {
  
          if (err) throw err;
  
          if (!item) 
          {
              res.json({ success: false, message: 'Authentication failed. item not found.' });
          } 
          else if (item) 
          {
  
              // check if password matches
              if (item.password != req.body.password) 
              {
                  res.json({ success: false, message: 'Authentication failed. Wrong password.' });
              } 
              else 
              {
  
                  // if item is found and password is right
                  // create a token
                  var token = jwt.sign(item, app.get('superSecret'), {
                      expiresIn: 86400 // expires in 24 hours
                  });
  
  
  
                      res.json({
                          success: true,
                          message: 'Enjoy your token!',
                          token: token
                      }); 
  
  
  
  
  
              }       
  
          }
  
      });
  });
  </code></pre>
  
  <p>Middleware which checks the token is correct is,</p>
  
  <pre><code>apps.use(function(req, res, next) {
  
      // check header or url parameters or post parameters for token
      var token = req.body.token || req.params.token || req.headers['x-access-token'];
  
      // decode token
      if (token) 
      {
  
          // verifies secret and checks exp
          jwt.verify(token, app.get('superSecret'), function(err, decoded) {          
              if (err) 
              {
                  return res.json({ success: false, message: 'Failed to authenticate token.' });      
              } 
              else 
              {
                  // if everything is good, save to request for use in other routes
                  req.decoded = decoded;  
                  next();
              }
          });
  
      } 
      else 
      {
  
          // if there is no token
          // return an error
          return res.status(403).send({ 
              success: false, 
              message: 'No token provided.'
          });
  
      }
  
  });
  </code></pre>
  
  <p>Finally the GET method script is,</p>
  
  <pre><code>app.get('/display', function(req, res) {
      Item.find({}, function(err, items) {
  
  
  
              $http.defaults.headers.common['X-Access-Token']=token;
  
              res.json(items);
  });
  });
  </code></pre>
  
  <p>But it always failed to authenticate. Please any one help me to solve this issue. I am really stucked here.</p>
  
  <p>It always shows only the following authentication failed message.</p>
  
  <pre><code>{"success":false,"message":"No token provided."}
  </code></pre>