【问题标题】:How can I call localhost from localhost (laravel, nodejs)?如何从本地主机(laravel,nodejs)调用本地主机?
【发布时间】:2016-03-23 22:05:27
【问题描述】:

我正在用 node 和 laravel 编写一个应用程序。我正在运行解析为 http://localhost:8000 的小型 laravel 本地服务器。我还在 localhost:3000 上运行节点服务器。然后尝试从第二个服务器调用第一个服务器。这是 NodeJs 代码:

var restify = require('restify');

var server = restify.createServer();

server.listen(3000, function() {
  console.log('%s listening at %s', server.name, server.url);
});

这里是我发出 http 请求的地方:

var http = require('http');


module.exports = {
    call: function (host, path) {
        var options = {
          host: host,
          path: path,
          port: 8000,
          method: 'GET'
        };

        callback = function(response) {
          var str = '';

          response.on('data', function (chunk) {
            str += chunk;
          });

          response.on('end', function () {
            return str;
          });
        }

        http.request(options, callback).end();
    }
}

这是我实际拨打的电话:

httpCaller.call('http://localhost', '/fire');

我在命令行上得到以下响应:

Error: getaddrinfo ENOTFOUND http://localhost http://localhost:8000
    at errnoException (dns.js:26:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)

我尝试删除 http:// 并调用本地主机,结果如下:

Error: connect ECONNREFUSED 127.0.0.1:8000
    at Object.exports._errnoException (util.js:890:11)
    at exports._exceptionWithHostPort (util.js:913:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)

我该怎么做?

【问题讨论】:

    标签: php node.js laravel networking localhost


    【解决方案1】:

    尝试使用http.get 函数?

    http.get('http://localhost:8000/fire', (res) => {
      console.log(`Got response: ${res.statusCode}`);
      // consume response body
      res.resume();
    }).on('error', (e) => {
      console.log(`Got error: ${e.message}`);
    });
    

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 1970-01-01
      • 2019-07-04
      • 2011-03-09
      • 2014-08-18
      • 2022-11-22
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多