问题
I am running server.coffee
from this page: https://github.com/xenph/foaas
From the command line, you can do:
curl http://localhost:5000/off/Name1/Name2
!@#$ off, Name1. - Name2
But I'm using the code from this page: http://coffeescriptcookbook.com/chapters/networking/basic-http-client
http = require 'http'
http.get { host: 'http://localhost:5000/off/Name1/Name2' }, (res) ->
data = ''
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
console.log data
The error I get is:
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
Which leads me to believe that it's not finding the url.
What can I do?
回答1:
try
http.get 'http://localhost:5000/off/Name1/Name2', (res) ->
or
http.get { hostname: 'localhost', port: 5000, path: '/off/Name1/Name2' }, (res) ->
instead of
http.get { host: 'http://localhost:5000/off/Name1/Name2' }, (res) ->
maybe?
回答2:
Firstly extract a little more info from exceptions:
.on('error',function(e){
console.log("Error: " + hostNames[i] + "\n" + e.message);
console.log( e.stack );
});
Secondly as curl is working we can assume that the service is up and accessible from localhost so there must be a deeper issue. What flavour of linux are you using? I've solved similar issues before by disabling firewall (iptables) and selinux.
Also double check to make sure that you have dns configured and that it will return 127.0.0.1 for localhost. Use nslookup.
来源:https://stackoverflow.com/questions/18780669/coffeescript-http-client