Http request to local server


#1

Hi everyone.

I have build an application for my android phone, which is call “remote”. The purpose is to send command to a nodeJS server on my computer. The server will then treats the request and send a response in json format. The server work correctly, I tested it in my browser. However, when I try to access the server with my application remote on my android device (I builded, signed and installed it), the application failed to made connection and the server received nothing.

Server code :

var http = require('http'); var url = require('url'); var querystring = require('querystring'); var fs = require('fs'); var PASSWORD = "TEST"; // quick password setting var server = http.createServer(function(req, res) { var path = url.parse(req.url).pathname; var parameters = querystring.parse(url.parse(req.url).query); switch (path){
case '/login': // .../login?password=my_password

  if ('password' in parameters){

    console.log('Password received:'+parameters['password']);

    if (parameters['password'] == PASSWORD) {

      console.log('good password');

      res.writeHead(200, {"Conten-Type": "application/json"});
      fs.readFile('./data/homeconfig.json', function(err, data) { // some json data
        var json = JSON.stringify(data);
        res.end(JSON.stringify(data));
      });
    }
    else {
      console.log('bad password');

      res.writeHead(404, {"Content-Type": "text/plain"});
      res.end("Bad password");
    }
  }
  else {

    console.log('no password!');
    res.end();
  }
  break;

case '/exe': // .../exe?path=some_path_to_script

  // will simply show the path of the script for now
  res.writeHead(200, {"Content-Type": "application/json"});
  console.log(parameters);

  res.end();
  break;

default:
  res.writeHead(404);
  res.end();
  break;
}

});

server.listen(80);

Login Controller on client side :

.controller('LoginCtrl', function($scope, $state, $http, StorageService, ServerConfig, HomeConfig, OptionsConfig) {

$scope.submit = function(ip, port, password) {
// ip is the Ipv4 private adresses : 192.168.X.X
// port in this case is 80 to connect to the server
// password in this case is TEST

ServerConfig.URL = "http://"+ip+":"+port;

console.log('Submit called');

/*
  I have tried first with :
  $http.get(ServerConfig.URL+"/login", {params: {"password": password}})
  and decided to change to what is now to see if it changes...
*/
$http.get(ServerConfig.URL+"/login?password="+password) // in this case : http//192.168.X.X:80/login?password=TES
  .success(function(data) {
    console.log('Request success!');
    HomeConfig = JSON.parse(data);
    $state.go('tab.home');
  })
  .error(function(err) {
    console.log('Request failed!');
    console.log(err);
  });

};

})

Emulate consol output:

$ ionic build android
$ ionic emulate android -lcs
(…)

0 962487 log Submit called
1 962620 log Request failed!
2 962622 log

$ ionic run android
(Server received the request but no response again)

Server side :

Password received:TEST
good password

So the server did received the request but the client failed to fetch the response.
However like I said, when I try it on device, the server received nothing.

Anyone has an idea? I’m new to the angularJS world, so if you have a solution, details would be great :slight_smile:


#2

This does not look like Ionic2 to me.


#3

is this ip accessible from your mobile browser?
if not try to use a domain or public ip
and this is definitely not ionic2 its v1


#4

Yeah it work fine in my mobile browser. And yeah my bad, I though I was working on Ionic2…


#5

Should I go on ionic2?