加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

node.js – 无法连接到Xcode中的本地服务器

发布时间:2020-12-14 18:09:50 所属栏目:百科 来源:网络整理
导读:尝试从 Xcode连接到本地服务器.我已将Alamofire Pod导入到我的Xcode项目中,并在xcode中运行以下命令 Alamofire.request(.GET,"http://localhost:3000",parameters: ["code": "123"]).responseJSON { response in print ("Hello",response) } 我在iOS设备上运
尝试从 Xcode连接到本地服务器.我已将Alamofire Pod导入到我的Xcode项目中,并在xcode中运行以下命令

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

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

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

$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,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: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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读