简体   繁体   English

从iOS模拟器访问本地node.js服务器?

[英]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. 我们有一些由Node.js服务器产生的服务,这些服务本地托管在我们的计算机上,并使用npm start初始化。

  2. An IONIC application that uses data from this server... 使用该服务器中数据的IONIC应用程序...

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. 使用$ ionic serve,该应用程序显然可以访问NODE.JS服务器,因为它们在相同的上下文中运行...不幸的是,当使用iOS模拟器($ ionic emulation ios)对其进行仿真时,无法访问该服务器。

Is there any way to keep the localhost context available to the iOS emulator? 有什么办法可以使localhost上下文可用于iOS仿真器?

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. 一种方法是获取您的本地网络 IP地址(可能由DHCP分配),并在仿真时将应用程序的端点设置为该IP。

You are on the same network, but not on the same "localhost" context. 您在同一网络上,但不在同一“ localhost”上下文中。

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. 如果您没有配置环境变量,那么另一个选择是使用ionic.Platform.platform()实用工具来帮助确定您的上下文是什么。 ( 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" 要在Mac OSX上找到本地IP地址,您可以打开系统偏好设置>网络,然后单击您的首选连接(例如wi-fi),该IP地址将显示在“关闭Wi-Fi”下方的顶部

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 在Windows上确定IP的一种简单方法是打开命令提示符(在开始搜索中为cmd),然后输入ipconfig并获取IPV4地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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