node.js net.connect() error in spite of try-catch - javascript

This code is producing an error in spite of a try-catch block... It should send a error message, if the server isn't reachable and a positive message, if ist is.
try {
net.connect(25565, "mc.hypixel.net", () => {
console.log("connected");
});
} catch (err) {
console.log("not connected");
}
This is the error:
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 104.16.78.21:25565
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '104.16.78.21',
port: 25565
}
Thank you for your help :)

hmm.. I believe the following answer is what net suggests
try this
net.connect(25565, "mc.hypixel.net", () => {
console.log("connected");
})
.on('error', (err)=>console.log("not connected"));

Related

node imap ssl error while connecting to gmail

Im getting the following error when attempting to connect to gmail through node:
node:events:368
throw er;
// Unhandled 'error' event
^
Error: 9C470000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:355:
at Socket.ondata (node:internal/js_stream_socket:77:22)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
Emitted 'error' event on Connection instance at:
at TLSSocket._onError (C:\Users\my-app1\node_modules\imap\lib\Connection.js:151:10)
at TLSSocket.emit (node:events:390:28)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
library: 'SSL routines',
reason: 'wrong version number',
code: 'ERR_SSL_WRONG_VERSION_NUMBER',
source: 'socket'
}
Node.js v17.3.1
PS C:\Users\my-app1\server-app> node imap
node:events:368
throw er; // Unhandled 'error' event
^
Error: 98400000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:355:
at Socket.ondata (node:internal/js_stream_socket:77:22)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
Emitted 'error' event on Connection instance at:
at TLSSocket._onError (C:\Users\my-app1\node_modules\imap\lib\Connection.js:151:10)
at TLSSocket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
library: 'SSL routines',
reason: 'wrong version number',
code: 'ERR_SSL_WRONG_VERSION_NUMBER',
source: 'socket'
}
Node.js v17.3.1
This is the code:
const Imap = require('imap');
const simpleParser = require('mailparser').simpleParser;
const fs = require('fs');
const imap = new Imap({
user: 'your-email-address',
password: 'your-email-password',
host: 'imap.example.com',
port: 993,
tls: true,
tlsOptions: {
rejectUnauthorized: false,
ca: [ fs.readFileSync('path/to/ca.crt') ]
}
});
imap.once('ready', function() {
imap.openBox('INBOX', true, function(err, box) {
if (err) throw err;
imap.search([ 'UNSEEN', ['SINCE', 'January 15, 2021'] ],
function(err, results) {
if (err) throw err;
if (results.length > 0) {
var f = imap.fetch(results, { bodies: '' });
f.on('message', function(msg, seqno) {
msg.on('body', function(stream, info) {
simpleParser(stream, (err, mail) => {
console.log(mail.subject);
console.log(mail.text);
});
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all unseen messages.');
imap.end();
});
} else {
console.log('No new messages.');
imap.end();
}
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended.');
});
imap.connect();
Any help would be greatly appreciated.

I have a problem with getting started sequelize with postgres

i wrote this code from sequelize documentation.
i created a databade in pgadmin with the name of users and pasword 12345 and o thought it should be connected to the db. but it does not. at first it write the " Connection has been established successfully. "
in the terminal but then i catch an error.
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('postgres://Admin:12345:5432/users')
try {
sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
the error:
reject(new sequelizeErrors.HostNotFoundError(err));
^
HostNotFoundError [SequelizeHostNotFoundError]: getaddrinfo ENOTFOUND admin
at Client._connectionCallback (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:133:24)
at Client._handleErrorWhileConnecting (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\client.js:305:19)
at Client._handleErrorEvent (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\client.js:315:19)
at Connection.emit (node:events:527:28)
at Socket.reportStreamError (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\connection.js:52:12)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
parent: Error: getaddrinfo ENOTFOUND admin
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'admin'
},
original: Error: getaddrinfo ENOTFOUND admin
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'admin'
}
}
where is the problem?

Node.js 'request' unhandled error, node:events:368

I'm getting a reoccuring error that crashes my program.
In the program, I'm consistantly making async requests (one by one I should say)
to download specific images off the internet, this is my code for the request:
const urlDownload = (uri, filename, callback) => {
try {
console.log('start url');
console.log(uri);
request.head(uri, function(err, res, body){
if(err) {
console.log('request error!');
console.log(err);
} else {
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
console.log('end url');
}
// console.log('content-type:', res.headers['content-type']);
// console.log('content-length:', res.headers['content-length']);
});
} catch(e) {
console.log('urlDownload issue');
console.log(e);
}
}
Now, sometimes it doesn't crash, but I feel like 50% or more of the times it does with this exact error:
node:events:368
throw er; // Unhandled 'error' event
^
Error: aborted
at connResetException (node:internal/errors:691:14)
at TLSSocket.socketCloseListener (node:_http_client:407:19)
at TLSSocket.emit (node:events:402:35)
at node:net:687:12
at TCP.done (node:_tls_wrap:580:7)
Emitted 'error' event on Request instance at:
at Request.onerror (node:internal/streams/legacy:62:12)
at Request.emit (node:events:390:28)
at IncomingMessage.<anonymous> (/home/mrz/Desktop/DEVELOPMENT/node_modules/request/request.js:1079:12)
at IncomingMessage.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
}
I don't know what to do, tried to search about it and found nothing really,
nothing wrong with the URL it tries to download too!
based on the console logs I have made, it seems like it crashes AFTER it ends the request process successfully I think, because I do get the output, and then it crashes, that's just speculations, I have no idea what's going on.

net.createConnection ECONNREFUSED

net.createConnection always seems to give ECONNREFUSED. I did a tcpdump and don't quite know what I'm looking for.
Code:
const net = require('net');
const client = net.createConnection({ port: 8124}, () => {
// 'connect' listener.
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('error', function(e) {
console.log(e);
console.log(e.message);
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
Output:
Error: connect ECONNREFUSED 127.0.0.1:8124
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8124
}
connect ECONNREFUSED 127.0.0.1:8124
Link to the pcap file to view the packets Click me
Use a tracer like tcpdump to see what is actually going on. The message, "connection refused," is actually a bit misleading. It doesn't actually mean that the host has identified you and is now refusing to talk to you.
Make sure you have a server running on port 8124 of your local machine (127.0.0.1). ECONNREFUSED means you dont.

Sequelize Connection Error when creating model

When I execute this program everything works fine:
const Sequelize = require("sequelize");
const Conn = new Sequelize("data","postgresql","postgresql", {
'host' : "localhost",
'dialect' : "postgres",
'port' : "1337"
})
// piece of code to be added here
Conn.sync().then(function(){
console.log('DB connection sucessful.');
}, function(err){
console.log(err);
});
the Console logs: DB connection successful.
When I add this line however:
const User = Conn.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
}
});
I get this error message:
{ SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:1337
at connection.connect.err (G:\projects\investcrypto\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:95:24)
at Connection.connectingErrorHandler (G:\projects\investcrypto\node_modules\pg\lib\client.js:123:14)
at emitOne (events.js:115:13)
at Connection.emit (events.js:210:7)
at Socket.<anonymous> (G:\projects\investcrypto\node_modules\pg\lib\connection.js:71:10)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'SequelizeConnectionRefusedError',
parent:
{ Error: connect ECONNREFUSED 127.0.0.1:1337
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 1337 },
original:
{ Error: connect ECONNREFUSED 127.0.0.1:1337
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 1337 } }
Versions:
npm: 5.3.0
node: 8.2.1
sequelize: 4.8.3
pg: 7.3.0
pg-hstore 2.3.2
Try making the port 5432. That is the default port for postgres. Make sure the database data was created by the user postgresql and that the password is indeed postgresql. Depending how the DB was created you might be able to just set username and password to null
Also, the Sequelize cli is really helpful for doing a lot of the boiler plate code

Categories