http.get if-else based on if user is authenticated or not
<p>Can you make an if-else statement to decide what to respond with based on if a function returns a status code or not?</p>

<p>nodejs/server.js</p>

<pre><code>app.get('/api/videos', function (req, res) {
    if(ensureAuthenticated){
        Video.find({}, { _id: 0, iframe: 1, title: 1 }, function (err, docs) {
            res.json(docs);
        });
    }else{
       Video2.find({}, { _id: 0, iframe: 1, title: 1 }, function (err, docs) {
            res.json(docs);
        });
});

function ensureAuthenticated(req, res, next) {
  if (!req.header('Authorization')) {
    return res.status(401).send({ message: 'Please make sure your request has an Authorization header' });
  }
  var token = req.header('Authorization').split(' ')[1];

  var payload = null;
  try {
    payload = jwt.decode(token, config.TOKEN_SECRET);
  }
  catch (err) {
    return res.status(401).send({ message: err.message });
  }

  if (payload.exp &lt;= moment().unix()) {

    return res.status(401).send({ message: 'Token has expired' });
  }
  req.user = payload.sub;
  req.role = payload.role;
  next();
}
</code></pre>

<p>to only allow logged in users have access to the http.get we can do </p>

<pre><code>app.get('/api/videos', ensureAuthenticated, function (req, res)
</code></pre>

<p>How would I do the http.get and then check ensureAuthenticated before responding with either Video.find() if authenticated or Video2.find() when not authenticated?</p>