Search code examples
javascriptangularjsnode.jsmean-stackangularjs-factory

AngularJS : factory $http.get request keep getting html file as return


may i know why i keep getting html file as return from my angular factory ?

this is my backend route

function ensureAuthenticated(req, res, next) {
  if (!req.headers.authorization) {
    return res.status(401).send({ message: 'Please make sure your request has an Authorization header' });
  }
  var token = req.headers.authorization.split(' ')[1];
  var payload = jwt.decode(token, config.TOKEN_SECRET);
  if (payload.exp <= moment().unix()) {
    return res.status(401).send({ message: 'Token has expired' });
  }
  req.user = payload.sub;
  next();
}

app.get('/api/me', ensureAuthenticated, userHandler.getMe);

this is the getMe function calling from userHandler

 this.getMe = function(req, res, next){
          // retrieve data from database by using req.user as id
          User.findById(req.user, function(err, user) {
            res.send(user);
         });
    }

in my service.js( i have try debug in here by changing the route '/api/me' to some other route it still return status 200 when there's no such route exist in route on my back end.

app.factory('Account', function($http) {
    return {
      getProfile: function() {
        return $http.get('/api/me'); 
      },
      updateProfile: function(profileData) {
        return $http.put('/api/me', profileData);
      }
    };
  });

in my controller

$scope.getProfile = function() {
      Account.getProfile()
        .success(function(data) {
          $scope.user = data;
          console.log(data) // this print out the html file 
        })
        .error(function(error) {
          alert("something is wrong when retriving your data");
        });
    };
    $scope.getProfile();

console.log(data) give me this enter image description here

in the network tab enter image description here

can anyone help me with this ? may i know is there any method to debug this kind of problem ? thanks !


Solution

  • ended up it caused by my nodejs app.js file i mess up the order of the line.

      "use strict";
    // all environments
    app.use(compress());
    app.use(require('prerender-node'));
    app.set('port', process.env.PORT || 3000);
    app.use(morgan('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use('/static', express.static(path.join(__dirname, '/public')));
    app.use(favicon());
    app.use(errorHandler());
    app.enable('trust proxy');
    
    //for html5 mode 
    app.get('*',function(req, res){ // this 
      res.sendFile('master.html', { root: path.join(__dirname, '/public/views/') });
    });
    routes(app, mongoose); // and this
    mongoose.connect(config.MONGO_URI);
    mongoose.connection.on('error', function() {
      console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
    });
    
    http.createServer(app).listen(app.get('port'),function(){
      console.log('Express server listening on port ' + app.get('port'));
    });
    

    it should be

    routes(app, mongoose); // this 
     //for html5 mode 
     app.get('*',function(req, res){ // and this 
     res.sendFile('master.html', { root: path.join(__dirname, '/public/views/') });
     });
    

    i'm very sorry for wasting your time. i really do, however, i'm really grateful for the effort you guys dedicated to help me solve this question. thanks !