Error: connect ECONNREFUSED issue in nodejs
while I am trying to execute the following code segment, I am getting the error... What is wrong with my code ?
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:646:11)
at Object.afterConnect [as oncomplete] (net.js:637:18)
The code is to get the HTML contents from the URL
function request1() {
http.get('http://ggggg/status', function(res){
var str = '';
console.log('Response is '+res.statusCode);
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
str += chunk;
});
res.on('end', function () {
console.log(str);
});
});
}
request1()
The code for collecting the response seems fine, it's the request causing the problem. You are not passing the authorisation header which is required. If you visit the url in the browser you'll see what's required.
This can also be verified by using curl:
$ curl http://integration.twosmiles.com/status
HTTP Basic: Access denied.
Also see: http://en.wikipedia.org/wiki/Basic_access_authentication
Error : ECONNREFUSED
Hope this helps !
Your code seems good. Actually ECONNREFUSED error comes when the server is not able to connect to a client.
In the code, http.get('http://ggggg/status', function(res){ … }
http.get()
takes the first argument as the url which indicates that you are trying to connect that particular server which is currently listening(foreign host).
For example:
var http = require('http');
function request1(){
http.get('http://www.google.com', function cb1(a) {
var str;
a.on('data', function cb2(data) {
// console.log(data);
str +=data;
});
a.on('end', function() {
console.log(str);
});
});
}
request1();
链接地址: http://www.djcxy.com/p/87246.html
上一篇: 写入流发出错误后恢复流程?