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

cni*_*s27 6 xcode localhost node.js swift alamofire

尝试从Xcode连接到本地服务器.我已将Alamofire Pod导入到我的Xcode项目中,并在xcode中运行以下命令

Alamofire.request(.GET, "http://localhost:3000" , parameters: ["code": "123"]).responseJSON {
                response in
                print ("Hello", response)
            }
Run Code Online (Sandbox Code Playgroud)

我在iOS设备上运行时收到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.}
Run Code Online (Sandbox Code Playgroud)

我知道当地正在服役.当我在命令行上调用以下函数时:

$ node index.js
Running at http://localhost:3000
Run Code Online (Sandbox Code Playgroud)

在浏览器中显示以下内容:

Cannot GET /
Run Code Online (Sandbox Code Playgroud)

我的.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);

 });

});
Run Code Online (Sandbox Code Playgroud)

Bor*_*rov 13

以下是评论的讨论摘要:

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

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

3)要在设备上进行测试 - 将本地主机地址替换为本地网络IP地址.本地网络地址通常看起来像192.168.xxx.xxx可以在网络设置中找到.

要在生产环境中实际运行此设置,您需要一台运行node.js应用程序的服务器,以获得公共真实IP地址或域名,并使用类似的方式连接到该服务器http://my.app.domain.or.ip:3000.