Node.js getaddrinfo ENOTFOUND when there is no internet connection - node.js

Normally, everything works, but when there is no internet connection my app throws an error:
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
/* my code - putting this to try...catch have no effect: */
var http = require('http');
// (...)
var req = http.request(options, response => {
/* ... */
});
req.write(data);
req.end();
So what can I do when internet connection shuts down and I would like to prevent my app from stopping?

To prevent your app from stopping when external server's internet connection is down, you can catch getaddrinfo ENOTFOUND error and return error message to your end client. Please check this SO on how to catch getaddrinfo ENOTFOUND error.
In your case, the code would be:
var http = require('http');
// (...)
var req = http.request(options, response => {
/* ... */
});
req.on('error', function (err) {
// Check error type and console.log corresponding message.
});
req.write(data);
req.end();

Related

NodeJS TCP Port crashes

I am developing socket application in my server with node js. I am just listening 9000 port. I am checking the data that client sent to this tcp port, if a client made http request, I kick client from server. Because some bots in the internet does that and I dont want them in my system. Due to test purposes, I try to connect that port with a browser, hold down F5 refresh button continuously, then application crashes immediately. I am simulating the DDOS attacks in my port by this way. The error message as follows:
events.js:183
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at _errnoException (util.js:992:11)
at TCP.onread (net.js:618:25)
And here is my basic TCP listener code
var net = require("net");
var server = net.createServer();
server.on("error", err =>
{
console.log("error handled, error is : %s",err);
});
server.on("connection",function(socket)
{
var remoteAddress = socket.remoteAddress;
console.log("new client connection %s",remoteAddress);
socket.end();
});
server.listen(9000, function()
{
console.log("I am listening.");
});
What can be done to save TCP port from HTTP connections and internet bots?
Put this under socket.end():
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
full code:
var net = require("net");
var server = net.createServer();
server.on("error", err =>
{
console.log("error handled, error is : %s",err);
});
server.on("connection",function(socket)
{
var remoteAddress = socket.remoteAddress;
console.log("new client connection %s",remoteAddress);
socket.end();
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
});
server.listen(9000, function()
{
console.log("I am listening.");
});

Nodejs Event error Express

i have this code in express.
var express = require('express');
var http = require("http");
var https = require("https");
var app = express();
var optionsSB = {
host: 'domain.com',
path: '/wp-content/themes/domain/includes/ajax/get_properties.php'
};
var optionsLV = {
host: 'apiproperties.local',
path: '/properties/storeSB',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
https.get(optionsSB, function (https_res) {
var dataSB = "";
https_res.on("data", function (chunkSB) {
dataSB += chunkSB;
});
https_res.on("end", function () {
http.request(optionsLV, function(http_res){
var dataVL = "";
http_res.on("data", function (chunkVL) {
dataVL += chunkVL;
});
http_res.on("end", function () {
console.log(dataVL);
});
});
});
});
app.listen(3000, function () {});
I get this error
events.js:183
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
I already try some things but i dont know what is the problem, regards.
I follow some instruction from a tutorials and all works fine but that error i dont understand.
It will throw like this when you are getting an error in setting up the request with your https.get(), but you don't have any error handler to capture the error. You can supply an error handler:
https.get(...).on('error', function(err) {
// error here
console.log(err);
});
It appears that the specific error is ECONNREFUSED. It could be that the destination is not accepting your https connection or it could be that it doesn't like the way you were passing the options. Since all you have is a host and path, you can also just use the URL:
https.get("https://somedomain.com//wp-content/themes/domain/includes/ajax/get_properties.php", ...);

Node.js Error: connect ECONNREFUSED thrown by client

I have set up a node program (actually two, one for the server and one for the client) but I get this error from my client every time I run it:
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
The code for the server:
var net = require('net');
var server = net.createServer(function (socket) {
console.log('Connection from ' + socket.remoteAddress);
socket.end('hello world')
});
server.listen(7000, '0.0.0.0')
This works fine. As for my client code, not so much. Here is my client code:
var net = require('net');
var client = new net.Socket();
client.connect(7000, 'IP of server here'); // in my actual code, I used the actual ip, of course
client.on('data', function(data) {
console.log('Data: ' + data);
client.destroy();
});
client.on('close', function () {
console.log('Connection closed');
});
This is one of my first node programs, and it is my first using TCP, so expect a newbie mistake. Thanks in advance for the help.
You need to handle the "error" event to avoid the default exception throw:
client.on('error', (error) => {
// treat error here
})

NodeJsApp - Unable to make successful http connections

I'm as new to Node and when I trying to run my first ever simple node app that makes an http connection to www.google.com host. While I tried some of the solutions suggested on prior threads nothing really seemed to help. While the below error is not a rare case but need someone to advise me what's missing from my setup/env.
source code - test.js - as simple as below -
var http = require('http');
var options = { host: 'www.google.com'};
http.get(options, function(err, res) {
console.log("GOT ERR?", err);
console.log("GOT RES?", res);});
I get the below error.
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)
Your callback is wrong. Try to write your code as follow:
var http = require('http');
var options = { host: 'www.google.com'};
// notice that the callback only receives a res parameter
// errors are handled on an event below
var req = http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
});
// handle errors
req.on('error', function(e) {
console.log("Got error: " + e.message);
});
Here is the documentation for http.get: http://nodejs.org/api/http.html#http_http_get_options_callback
The error that you are seeing (throw er; // Unhandled 'error' event) is because you do not have an even handler for the error event. Notice the req.on('error') in my code to address this.

Node http.request not working

I am trying to create a simple server and a simple client with node.js using http module. Server is working fine but client is not. Please help me to find the bug...
Server Is :
var server = require('http').createServer();
server.on('request', function(req, res){
res.end("hello, world");
});
server.listen(4000);
Client Is :
var options = {
host : 'localhost',
port : 4000,
method : 'GET',
path " '/'
};
require('http').request(options, function(res){
console.log(require('util').inspect(res));
res.on('data', function(data){
console.log(data);
});
I am running them in different terminal windows as node server.js & node client.js.
I am getting below mentioned error on the client.js running terminal after around 10 mins.
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1473:15)
at Socket.socketOnEnd [as onend] (http.js:1569:23)
at Socket.g (events.js:175:14)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Thanks !
The request() method of the HTTP library will not automatically end requests, therefore they stay open, and time out. Instead, you should either end the request using req.end(), or use the get() method, which will do so automatically.
var http = require('http');
var req = http.request(options, function(res) {
// handle the resposne
});
req.end();
Or:
http.get(options, function(res) {
// handle the resposne
});

Resources