php - 如何从 localhost(laravel、nodejs)调用 localhost?

标签 php node.js laravel networking localhost

我正在使用 Node 和 Laravel 编写一个应用程序。我正在运行小型 laravel 本地服务器,该服务器解析为 http://localhost:8000 。我还在 localhost:3000 上运行 Node 服务器。然后尝试从第二个服务器调用第一个服务器。这是 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)

我该怎么做?

最佳答案

尝试使用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}`);
});

关于php - 如何从 localhost(laravel、nodejs)调用 localhost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36189602/

相关文章:

php - 在 Laravel 中连接数据库的最佳方式

node.js - 可以将 npm 3.x 与 nodejs 4.x 一起使用吗?

laravel - 无法在 laravel 中使用 smtp.zoho.com 发送电子邮件

php - contenteditable 在表 : select cells

php - 在 xampp linux 上设置现有的 Yii 项目

node.js - 在 React JS 中存储 API 根 Url 的最佳位置是什么?

PHP Artisan 使用 MAMP 和 Unix Socket 迁移

php - 如何确保JPEG图像有效并且可以被PHP处理?

php - Ajax 表单只能工作一次

node.js - Flowjs 文件上传 - AngularJS 和 Node