无法连接到 Xcode 中的本地服务器

Could Not Connect to Local Server in Xcode

正在尝试从 Xcode 连接到本地服务器。我已将 Alamofire Pod 导入到我的 Xcode 项目中,并在 xcode

中 运行 以下命令
Alamofire.request(.GET, "http://localhost:3000" , parameters: ["code": "123"]).responseJSON {
                response in
                print ("Hello", response)
            }

在 iOS 设备上 运行ning 时,我在 Xcode 中收到以下错误。

 FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x13d84f7f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost:3000/, NSErrorFailingURLKey=http://localhost:3000/, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}

我知道本地服务是 运行ning。当我在命令行上调用以下函数时:

$ node index.js
Running at http://localhost:3000

在浏览器中显示如下:

Cannot GET /

我的 .js 文件如下:

 var buffer = require('buffer');
 var bodyParser = require('body-parser');
 var crypto = require('crypto');
 var express = require('express');
 var request = require('request');
 var url = require('url');
 var app = express();

var config = {
  clientId: '',
  clientSecret: '',
  callbackUrl: '',
  encryptionSecret: '',
  endpoint: 'https://accounts.spotify.com',
 };

 var secretString = config.clientId + ':' + config.clientSecret;
 var authHeader = 'Basic ' + new  buffer.Buffer(secretString).toString('base64');

 // app.use(bodyParser.urlencoded({ extended: false })); // TODO - Figure out why this should be here
 app.use(bodyParser.json()); // TODO - Figure out why this should be here

 var server = app.listen(3000, function() {
  var address = server.address();

  console.log('Running at http://localhost:%s', address.port);
 });

 app.post('/swap', function(req, res) {

  console.log(req.body);

  if (!req.body || !req.body.hasOwnProperty('code')) {
    console.log('Swap: missing auth code');
    res.status(550).send('Permission Denied');

    return;
  }

  formData = {
    grant_type: 'authorization_code',
    redirect_uri: config.callbackUrl,
    code: req.body.code
  };

  console.log('Swap: POST to %s', url.resolve(config.endpoint, '/api/token'), formData);

  request.post({
    url: url.resolve(config.endpoint, '/api/token'),
    headers: {
      'Authorization': authHeader,
      'Content-Type': 'application/x-www-form-urlencoded',
  },
  form: formData,
  }, function(error, response, body) {

  if (error) {
     console.log('Swap: Error - ', error);
     res.status(500).send('Internal Server Error');

     return;
   }

    if (res.statusCode != 200) {
     debug('Swap: response: ', response.statusCode);
     res.status(550).send('Permission Denied');

    return;
    }

   var tokenData = JSON.parse(body);

   console.log('Swap: tokenData - ', tokenData);

   res.status(200).set({
     'Content-Type': 'application/json',
   }).send(tokenData);

 });

});

以下是评论中的讨论摘要:

1) 最初 node.js 应用程序只有 POST 请求的路由。这使得调试更加复杂。为 GET 请求添加路由,以便您可以从浏览器检查 http://localhost:3000。现在检查它是否在桌面浏览器和模拟器浏览器中工作,以确认一般节点应用程序可用性。

2) http://localhost:3000http://127.0.0.1:3000 等地址仅适用于 node.js 应用程序 运行ning 所在的同一台机器。这包括 iOS 模拟器,但不适用于设备上的此类地址。

3) 要在设备上进行测试 - 将本地主机地址替换为您的本地网络 IP 地址。本地网络地址通常类似于 192.168.xxx.xxx,可以在网络设置中找到。

要在生产环境中实际运行此设置,您将需要一个服务器,您可以在其中 运行 node.js 应用程序,以获得 public 真实 IP 地址或域名,您将使用 http://my.app.domain.or.ip:3000.

之类的内容连接到它