trying to connect to smtp server using nodemailer - node.js

Unable to connect to the SMTP server. Is there a better way to connect and check the validity of the SMTP server from the domain?
var net = require('net');
const dns = require('dns')
const SMTPConnection = require("nodemailer/lib/smtp-connection");
dns.resolveMx('gmail.com', function(err, addresses){
if(err) console.log(err);
// just taking the first mail server
h = addresses[0].exchange;
options = {
host:h,
port:25,
connectionTimeout: 3000
};
let connection = new SMTPConnection(options);
connection.connect(()=>{console.log('connected')});
connection.on('error', (err)=>{console.log(err)})
connection.on('end', ()=>{console.log('end')})
})
Error
{ Error: connect EHOSTUNREACH 142.250.11.26:25
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ESOCKET',
errno: 'EHOSTUNREACH',
syscall: 'connect',
address: '142.250.11.26',
port: 25,
command: 'CONN' }
end
I get the same error when I try with new net.Socket([options]) from the net module

Related

Connecting to redis on render.com

I received this error when I wanted to connect to redis instance created on render.com. It should be mentioned that I enabled the instance to connect servers outside render.com.
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
this is my redis config
import redis from "redis";
const client = redis.createClient({
url: process.env.REDIS_URL_EXTERNAL,
});
export default client;
and this is how I connected the client in the server.js file
(async () => {
await client.connect();
})();
I tried to connect to a Redis instance created on Render.com using the redis library in Node.js. I expected the connection to be successful, and for the Connected to Redis message to be logged to the console. However, I received the mentioned error.

Redis Overiding remote host in Nodejs

I am connecting to a remote Redis using the following code. The problem connecting because it does not accept the giving IP instead points to localhost.
const redisURL = "redis://20.213.158.211:6379" // random remote IP address
const client = redis.createClient({redisURL});
client.on('connect', function(){
client.select(5);
console.log('Connected to Redis');
});
client.on("error", (err) => console.log("Redis Client Error", err));
client.connect();
It throws the following error
Redis Client Error Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
The problem is it still trying to connect to 127.0.0.1(localhost) instead of the giving IP address above.

How to connect with mongoDB via SSH

const config = {
username: "username",
password: "password",
host: "0.0.0.0",
port: 22,
dstPort: 27017,
};
tunnel(config, function (error, server) {
if (error) {
console.log("SSH connection error: " + error);
}
console.log("SSH ok");
mongoose.connect(
"mongodb://127.0.0.1:27017/megaparking?retryWrites=true&w=majority'"
);
console.log("Connect ok");
var db = mongoose.connection;
db.on("error", console.error.bind(console, "DB connection error:"));
db.once("open", function () {
console.log("DB connection successful");
});
});
I want to connect with MongoDB installed on server, but I have the issue:
127.0.0.1:27017 is already in use cause
I was opening sever locally and I want connected with MongoDB on dedicated server, which has also 127.0.0.1 host and port 27017. What I should do with that code? Or maybe I should create a MongoDB connection string by this data?
The issue:
Error: listen EADDRINUSE: address already in use 127.0.0.1:27017
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at doListen (net.js:1503:7)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '127.0.0.1',
port: 27017
}
[nodemon] app crashed - waiting for file changes before starting...

ETIMEDOUT error on wikidata api call using request promise

Below is the snippet of nodejs to call wikidata api using request-promise module
var rp = require('request-promise');
var wikidataURL="http://www.wikidata.org/w/api.php?action=wbgetentities&props=labels|claims&languages=en&format=json&ids=Q1"
let promise=rp(wikidataURL).catch(function(e){
console.log(e);
});
For me, it worked normally till 27/1/2020. After that I get a ETIMEDOUT connection error. The same links would work on browser but not on nodejs. There is no proxy involved.
Error message:
{ Error: connect ETIMEDOUT 91.198.174.192:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '91.198.174.192',
port: 80 },
Is this something on my side or wikidata server?
So it seems only https is allowed now for wikidata api query after some update from their side

Connecting to postgres on Heroku with Node.js

I try to connect to my postgres database on Heroku by:
var pg = require('pg');
pg.defaults.ssl = true;
var pool = new pg.Pool(process.env.DATABASE_URL);
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
I have confirmed the value of proccess.env.DATABASE_URL it has the right format, and I can successfully connect to it via psql postgres://XXXXXXXX:XXXXXXXXXXXXXXX#XXXXXXXXXXXXX.eu-west-1.compute.amazonaws.com:5432/XXXXXXXX
This is the output, and what confuses me is the 127.0.0.1 reference if this output.
2016-06-27T14:58:52.714154+00:00 app[web.1]: Express server listening on port 18119
2016-06-27T14:59:29.175122+00:00 app[web.1]: error fetching client from pool { Error: connect ECONNREFUSED 127.0.0.1:5432
2016-06-27T14:59:29.175133+00:00 app[web.1]: at Object.exports._errnoException (util.js:949:11)
2016-06-27T14:59:29.175134+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:972:20)
2016-06-27T14:59:29.175135+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
2016-06-27T14:59:29.175136+00:00 app[web.1]: code: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]: errno: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]: syscall: 'connect',
2016-06-27T14:59:29.175138+00:00 app[web.1]: address: '127.0.0.1',
2016-06-27T14:59:29.175139+00:00 app[web.1]: port: 5432 }
Try this:
var pg = require('pg').native
pg.connect(process.env.databaseURL, function (err, conn, done) {
...
}

Resources