Kuja
Kuja

Reputation: 449

http request responds with html instead of json

I've made some vimeo api calls to get videos, but when I do a get request on the server it responds with the html on that path instead of the data from the server. I'm also using angular-client-side-auth (https://github.com/fnakstad/angular-client-side-auth). I'm new to this, so I'm struggling to understand why this happens.

server.js

app.get('/api/mostviewed', function (req, res) {
  MostViewed.find({}, { _id: 0, iframe: 1 }, function (err, docs) {
    res.json(docs);
  });
});

inside client-side-auth's routes.js there's this, which causes it all(This file is on the server-side):

{
    path: '/*',
    httpMethod: 'GET',
    middleware: [function(req, res) {
        var role = userRoles.public, username = '';
        if(req.user) {
            role = req.user.role;
            username = req.user.username;
        }
        res.cookie('user', JSON.stringify({
            'username': username,
            'role': role
        }));
        res.render('index');
    }]
}

How can I solve this? I want to maintain the path: '/*', or change it while keeping the function similar, so I can get my data from the server. Or is there a different way to solve this?

EDIT: Solution

{
   path: '/api/mostviewed',
   httpMethod: 'GET',
   middleware: [Video.getmostviewed]
},

inside Video.js I made this:

getmostviewed: function(req,res){
    MostViewed.find({}, { _id: 0, iframe: 1 }, function (err, docs) {
    res.json(docs);
  });
}

Upvotes: 1

Views: 682

Answers (1)

Mukesh Sharma
Mukesh Sharma

Reputation: 9022

In your case, client could not find logic corresponding to /api/mostviewed, thus reached /* and displayed html instead of json.

Possible solution

Add following similar logic before /*

{
   path: '/api/*',
   httpMethod: 'GET',
   middleware: [function(req, res) {  // sample middleware logic
       var role = userRoles.public, username = '';
       if(req.user) {
          role = req.user.role;
          username = req.user.username;
       }
       res.cookie('user', JSON.stringify({
        'username': username,
        'role': role
       }));
   }]
},

Upvotes: 1

Related Questions