简体   繁体   中英

Accesing local node.js server from iOS Emulator?

We are trying to figure out how to do this:

  1. We have some service produced by Node.js server, locally hosted on our computer and initialized with npm start.

  2. An IONIC application that uses data from this server...

Using $ionic serve, the app can obviously access the NODE.JS server because they are running on the same context... unfortunately, when emulating it with the iOS emulator ($ionic emulate ios), the server can't be accessed.

Is there any way to keep the localhost context available to the iOS emulator?

One way is to grab your local network IP address (likely assigned by DHCP), and set the endpoint for the application to be that IP when you're emulating.

You are on the same network, but not on the same "localhost" context.

This might look something like:

angular.module('MyIonicApp')
  .constant('apiEndpoint', function() {
      var env   = process.env.SENTRY_MODE;
      var dbURI = process.env.DB_URI;

      /** Live API **/
      if (env === 'prod')
           { return dbURI; }
      /** Local development **/
      else { return 'localhost'; }
  });

If you don't have environment variables configured, another option would be to use the ionic.Platform.platform() utility to help determine what your context is. ( Docs )

This is a temporary solution, but should get you up and running for tests.

To find your local IP address on Mac OSX you can open your system preferences > Network and click on your preferred connection (like wi-fi) and the IP address will be displayed at the top underneath "Turn Wi-Fi Off"

An easy way to determine your IP on windows is to open command prompt (cmd in the start search) and enter ipconfig and get your IPV4 address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM