2016-03-23 2 views
2

Ich schreibe eine Anwendung mit Knoten und Laravel. Ich betreibe einen kleinen lokalen Laravel-Server, der in http://localhost:8000 aufgelöst wird. Ich betreibe auch einen Knoten-Server auf localhost: 3000. Dann versuchen, den ersten Server von der Sekunde an zu rufen. Hier ist der NodeJS Code:Wie kann ich localhost von localhost (laravel, nodejs) aufrufen?

var restify = require('restify'); 

var server = restify.createServer(); 

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

Hier ist, wo ich die HTTP-Anforderung machen:

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

Dies ist der eigentliche Anruf ich, damit ich bin:

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

erhalte ich die folgende Antwort in der Befehlszeile:

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

Ich habe versucht, das Entfernen http: // und nur lokale Host-Aufruf und bekam folgende:

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) 

Wie kann ich das tun?

Antwort

0

Versuchen Sie mit der http.get Funktion?

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