Error: connect ECONNREFUSED issue in nodejs - node.js

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
Is usually arised when you are trying to access a port which is
already accessed by another program.
Double check on the port you are using & try using another port
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();

Related

Node.js v8.11.1 EPROTO error when attempting to POST using https module

I have a system that is running on Node 8.11.1 on AWS. There is a function that writes logs to another server. This function takes a request object that it logs.
My problem arises during the actual POST attempt, giving me the following error:
Error: write EPROTO 139746875082624:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:827:
I cannot see anything wrong with my code.
Why is the error occurring, and what can I do to fix it?
Here is the code inside of the function:
const https = require('https');
try
{
const postData = "New log finished " + JSON.stringify(request, null, 2);
const options =
{
hostname: LOG_DOMAIN,
port: LOG_PORT,
path: '/',
method: 'POST'
};
const req = https.request(options);
req.on('error', (e) =>
{
console.error("ERROR writing logs: " + e);
});
req.write(postData);
req.end();
}
catch (e)
{
console.log(e);
}
LOG_DOMAIN and LOG_PORT are variables passed to the function.
After further research, it appears that Aravind Voggu (see comments) was in the right vein: the error comes from attempts to use HTTPS for a server that only allows HTTP.
The OpenSSL dies when attempting to secure a connection to something that is unsecured.
The only change required to make my code work correctly was to remove the "s" from "https" in the locations used.
const http = require("http");
This issue can be encountered if server side ssl certificate gets expired

node, socket.io - connect to list of news feed urls?

I wish to connect to the list of news feed urls via node and get the real-time data via socket.io. For that I tried with single url in server.js as below:
var http = require("http");
var options = {
host: 'http://economictimes.feedsportal.com/c/33041/f/534037/'
};
http.get(options, function (http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});
When I execute node server.js, it throws me an error
"Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)"
Is there any way to pass each news feed url from an array to connect it via node and get latest news via socket.io ???
From the node doc for the http module, this is what a typical options object looks like:
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
Per the doc, the host option you are using should be A domain name or IP address of the server to issue the request to. Defaults to 'localhost'. So, it looks like you just aren't calling .get() correctly.
If you just want to pass the whole URL, then don't use the options object, just pass the URL like this and the method will parse the URL for you into the relevant parts:
http.get('http://economictimes.feedsportal.com/c/33041/f/534037/', function (http_res) {...});

node.js cannot make rest call with http

I am new to node.js and I am trying to make an http request. I followed a tutorial trying to call random.org. this is my app.js file:
var http = require('http');
//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.org',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
response.on('error', function () {
console.log("err");
});
}
http.request(options, callback).end();
So, anyway, when running this file using
node app.js
I get the following error to the console:
C:\Program Files\nodejs>node app.js
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
2 problems:
1. Why am I getting time out error (the site works - I checked)
2. Anyway - why am I not catching this error althought I have an error listener in the response.
Any possible help will be much appreciated!
Thanks
You are attaching the error listener to the response, but error happens in request itself. Do someting like this :
http.request(options, callback)
.on('error', function () {
console.log("err in request");
})
.end();
The error means the site is available, if not the error would show EHOSTUNREACH. ETIMEDOUT means that request was not responded fast enough. It needs some investigation, lke is the network down, try getting www.google.com. What is the timeout value it is considering ? etc.
OK. I tried to run this as standalone node js process without browser as background.
This is not recommended, and in the cases specified above the problem was that this headerless request lacked cookies.
If I would wrap everything in a createServer method, and upon request I would do the http.request with the same parameters + the cookies from the server request - this would work.

GeoJson from Geoserver using node.js

I am new to Node.js, learning with examples.
Here is what I am trying to do, I have a geoserver running to serve GeoJson, I want to call geoserver WFS url and get json data using node.js. Here is code, when I run it, I get :
getaddrinfo ENOENT
var http = require('http');
var options = {
host: "local:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=layername&outputFormat=JSON&cql_filter=id=1";
path: '/'
}
var request = http.request(options, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log(data);
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
Please guide me in right direction. Thank you.
You need to pass in the correct options:
host - should only be the host name
path - should the path to the resource on the host (all the stuff you have after the host name
method - should be GET or POST (GET in your case).
var options = {
host: "local:8080";
path: '/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=layername&outputFormat=JSON&cql_filter=id=1',
method: 'GET'
}
I was also trying to get a GeoJSON via Node.js and took a different approach; I used Express and sequelizejs. This allowed me to get objects directly from Postgres / PostGIS. I needed to do a little formatting client-side to form a valid GeoJSON from the express response.

Catching ECONNREFUSED in node.js with http.request?

I'm trying to catch ECONNREFUSED errors when using a HTTP client in node.js. I'm making requests like this:
var http = require('http');
var options = { host: 'localhost', port: '3301', path: '/', method: 'GET' };
http.request(options).on('response', function (res) {
// do some stuff
});
I can't figure out how to catch this error:
Error: connect ECONNREFUSED
at errnoException (net.js:614:11)
at Object.afterConnect [as oncomplete] (net.js:605:18)
If I do request.on('error', function () {});, it doesn't catch it. If I do it like this:
var req = request.on(etc)
req.on('error', function blah () {});
Then I get TypeError: Object false has no method 'on'.
Do I really have to do a top-level uncaught error thing to deal with this? At the moment whatever I do my whole process quits out.
Edit: I found some blog posts on how to do it by creating a connection object, calling request on that, and then binding to errors on the connection object, but doesn't that make the entire http.request() shortcut useless?
Any reason you're not using http://nodejs.org/docs/v0.6.5/api/http.html#http.request as your base? Try this:
var req = http.request(options, function(res) {
// Bind 'data', 'end' events here
});
req.on('error', function(error) {
// Error handling here
});
req.end();
Each call to http.request() returns its self.
So try it like this...
http.request(options.function(){}).on('error',function(){}).end();
I've got a solution for this, having tried all the suggestions on this (and many other) pages.
My client needs to detect a turnkey product that runs embedded windows. The client is served from a different machine to the turnkey.
The turnkey can be in 3 states:
turned off
booted into windows, but not running the turnkey app
running the turnkey app
My client sends a 'find the turnkey product' GET message to my nodejs/express service, which then tries to find the turnkey product via http.request. The behavior for each of the 3 use cases are;
timeout
ECONNREFUSED - because the windows embedded phase of the turnkey is
refusing connections.
normal response to request (happy day scenario)
The code below handles all 3 scenarios. The trick to catching the ECONNREFUSED event was learning that its handler binds to the socket event.
var http = require('http');
var express = require('express');
var url = require('url');
function find (req, res) {
var queryObj = url.parse(req.url, true).query;
var options = {
host: queryObj.ip, // client attaches ip address of turnkey to url.
port: 1234,
path: '/some/path',
}; // http get options
var badNews = function (e) {
console.log (e.name + ' error: ', e.message);
res.send({'ok': false, 'msg': e.message});
}; // sends failure messages to log and client
// instantiate http request object and fire it
var msg = http.request(options, function (response) {
var body = '';
response.on ('data', function(d) {
body += d;
}); // accumulate response chunks
response.on ('end', function () {
res.send({'ok': true, 'msg': body});
console.log('sent ok');
}); // done receiving, send reply to client
response.on('error', function (e) {
badNews(e);
}); // uh oh, send bad news to client
});
msg.on('socket', function(socket) {
socket.setTimeout(2000, function () { // set short timeout so discovery fails fast
var e = new Error ('Timeout connecting to ' + queryObj.ip));
e.name = 'Timeout';
badNews(e);
msg.abort(); // kill socket
});
socket.on('error', function (err) { // this catches ECONNREFUSED events
badNews(err);
msg.abort(); // kill socket
});
}); // handle connection events and errors
msg.on('error', function (e) { // happens when we abort
console.log(e);
});
msg.end();
}
For those not using DNS (you can also use request instead of get by simply replacing get with request like so: http.request({ ... })):
http.get({
host: '127.0.0.1',
port: 443,
path: '/books?author=spongebob',
auth: 'user:p#ssword#'
}, resp => {
let data;
resp.on('data', chunk => {
data += chunk;
});
resp.on('end', () => console.log(data));
}).on('error', err => console.log(err));

Resources