Failed to connect to local server in Xcode

Attempting to connect to a local server from Xcode. I imported Alamofire Pod into my Xcode project and executed the following command in xcode

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

I get the following error in Xcode when working on an iOS device.

 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.}

I know that the local service is working. When I invoke the following command on the command line:

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

The following is shown in the browser:

Cannot GET /

My .js file is as follows:

 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);

 });

});
+4
source share
1 answer

Here is a summary of the discussion from the comments:

1) node.js POST. . GET, http://localhost:3000 . , , node.

2) http://localhost:3000 http://127.0.0.1:3000 , node.js. iOS, .

3) - IP- . 192.168.xxx.xxx .

, , node.js, IP- , , - http://my.app.domain.or.ip:3000.

+10

Source: https://habr.com/ru/post/1624463/


All Articles