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.
Related
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
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) {...});
I try to put many callbacks in a callback, but the program will shut down after the return of several success requests and post "socket hang up" error. I try to collect data from response and output them at once, can someone tell me which part goes wrong...
By the way, I hide the details of request method, I promise the request method works on http call.
http.request(options1,function(data){
var condition=data.length;
var result=[];
data.foreach(item,function(data){
http.request(options2, function(data){
if(data) result.push(data);
condition--;
if(condition<=0) console.log(result);
}
});
});
for my http.request method
var request=function(options,callback){
http.request(options,function(res){
var body;
res.on('data',function(chunk){
body+=chunk;
});
res.on('end',function(){
callback(JSON.parse(body));
});
request.end();
};
That's not the correct usage of http.request().
The http.request() callback is passed an IncomingMessage object, not buffered response data.
EDIT: Your custom request() should look something like this, although you should handle errors too. Note the proper placement of request.end() and initializing var body = '':
function request(options, callback) {
http.request(options,function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data',function(chunk) {
body += chunk;
}).on('end',function() {
callback(JSON.parse(body));
});
}).end();
}
You're missing .end() for your requests so that node.js knows you are ready to actually send the HTTP request: http.request(..., ....).end();. This is the cause of your particular error... the server hangs up the connection because it got tired of waiting for your request after the TCP connection was opened.
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();
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));