Normalmente, todo funciona, pero cuando no hay conexión a Internet, mi aplicación arroja un 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();Entonces, ¿qué puedo hacer cuando la conexión a Internet se apaga y me gustaría evitar que mi aplicación se detenga?
Para evitar que su app se detenga cuando la conexión a Internet del servidor externo no funciona, puede detectar el error getaddrinfo ENOTFOUND y devolver el mensaje de error a su cliente final. Consulte este SO sobre cómo detectar el error getaddrinfo ENOTFOUND .
En tu caso, el código sería:
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();