Tuesday, 11 April 2017

Why adding a get parameter forces middleware execution

I have got a middleware like this

// route middleware to verify a token
app.use(function(req, res, next) {

  console.log(req.baseUrl);

  // check header or url parameters or post parameters for token
  var token = req.body.token || req.query.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.'
    });

  }
});

This route doesn't responds as No token provided

app.get('/verifyAccount', function (req, res) {
  res.json({ message: 'Welcome to verify account!' });
});

But the following does:

app.get('/verifyAccount/:id', function (req, res) {
  res.json({ message: 'Welcome to verify account!' });
});

Why adding that parameter forcing middleware execution? The middleware code is at the bottom in the code file and the get paths are upwards

What is that concept behind?



via Student

No comments:

Post a Comment