Monday, 24 April 2017

Not receiving proper data when routing as asterisk *

I want to define my '/' route as '*' because I am using $locationProvider.html5Mode(true); in Angular.

By defining my main route as an asterisk my website keeps it's state when refreshing. Otherwise it gives a "CANNOT GET" error if I am refreshing for e.g. on the /login page.

But when I define my main route as an asterisk it doesn't receive the proper data from the server.

get '*'

router.get('*', function(req, res) {
  res.render('./index');
});

router.get('/test', function(req, res) {
  res.json({go : 'Welcome to the test page.'});
});

Gives:

Object { data: "<!DOCTYPE html> <html lang="en" ng-…", status: 200, headers: wd/<(), config: Object, statusText: "OK" }

get '/'

router.get('/', function(req, res) {
  res.render('./index');
});

router.get('/test', function(req, res) {
  res.json({go : 'Welcome to the test page.'});
});

Gives

Object { data: "test", status: 200, headers: wd/<(), config: Object, statusText: "OK" }

By calling a button with this function in Angular:

my.test = function() {
      $http.get('/test')
        .then(handleSuccess)
        .catch(handleError);

        function handleSuccess(response) {
          console.log(response);
        }
        function handleError(error) {
          console.log(error);
        }
    }



via Soundwave

No comments:

Post a Comment