Ionic + Express 4


#1

My angular project ist running on port 8100 and the Express 4 server uses port 8080.
How to make cross-domain access possible?

I added the following lines to my server.js (Express 4):

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

customersFactory.js

var path = 'http://localhost:8080';

  factory.getCustomers = function() {
    return $http.get(path + '/customers');
  };

  factory.getCustomers = function(customersId) {
    return $http.get(path + '/customer/' + customersId);
  };

The following error occurs:

GET http://localhost:8080/customer/undefined 404 (Not Found)


#2

Your customersId is undefined when you call getCustomers because you are overriding the previous definition of factory.getCustomers, try calling your method getCustomersById and updating your code, it should work then.

Or you could try to do that

  factory.getCustomers = function(customersId) {
    if(customersId != "undefined") {
        return $http.get(path + '/customer/' + customersId);
    }

   return $http.get(path + '/customers');
  };

#3

Oh I’m so blind… changing getCustomers to getCustomer without s, helped. Sometimes it’s useful, if someone else, is seeing your code.
Thanks!