NODEJS TEDIOUS reconnect - node.js

I've connected to an SQL Server using Tedious/NodeJS. Connection and queries are working fine (in command-line) until the connection (LAN or WIFI) goes down.
Then, the nodejs script in cmd throws an unhandled error event .
How I can catch disconnections and reconnect, while keeping the script running in console?
events.js:174<br>
throw er; // Unhandled 'error' event<br>
^<br>
ConnectionError: Connection lost - read ECONNRESET<br>
at ConnectionError (dev node\node_modules\tedious\lib\errors.js:13:12)<br>
at Connection.socketError (dev node\node_modules\tedious\lib\connection.js:1297:54)<br>
at Socket.socket.on.error (dev node\node_modules\tedious\lib\connection.js:1128:14)<br>
at Socket.emit (events.js:203:15)<br>
at emitErrorNT (internal/streams/destroy.js:91:8)<br>
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)<br>
at process._tickCallback (internal/process/next_tick.js:63:19)<br>
Emitted 'error' event at:<br>
at Connection.socketError (dev node\node_modules\tedious\lib\connection.js:1297:12)<br>
at Socket.socket.on.error (dev node\node_modules\tedious\lib\connection.js:1128:14)<br>
[... lines matching original stack trace ...]<br>
at process._tickCallback (internal/process/next_tick.js:63:19)<br>
Win7
node V10.16.0
Microsoft SQL Server Standard Edition (64-bit)

Related

node:events:371 throw er; // Unhandled 'error' event ^ Error: read EFAULT

I've been getting this error and have tried reinstalling node/vscode already to no avail. This happens maybe 30% of the time after I save my changes in vscode and it ALWAYS crashes my node server.
Nodemon doesn't turn my server back on and it gets really tedious when trying to code. Does anyone have any solutions to this problem?
I'm running on a Macbook Air 2020 with a M1 chip if that makes a difference.
(Using express / nodejs ) node v16.6.0
I've tried killall node, I've tried changing port number. same errors
throw er; // Unhandled 'error' event
^
Error: read EFAULT
at Pipe.onStreamRead (node:internal/stream_base_commons:211:20)
Emitted 'error' event on Socket instance at:
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) {
errno: -14,
code: 'EFAULT',
syscall: 'read'
}```

Node.js Error: Unexpected server response: 301 - Running a Node.js Websocket server on Namecheap Webhost

I'm trying to setup a Node.js Websocket server over at my webhost (Namecheap). However, I am getting this error:
events.js:292
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 301
at ClientRequest.<anonymous> (D:\****\node_modules\ws\lib\websocket.js:576:7)
at ClientRequest.emit (events.js:315:20)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:596:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)
at TLSSocket.socketOnData (_http_client.js:469:22)
at TLSSocket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at TLSSocket.Readable.push (_stream_readable.js:212:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on WebSocket instance at:
at abortHandshake (D:\****\node_modules\ws\lib\websocket.js:694:15)
at ClientRequest.<anonymous> (D:\****\node_modules\ws\lib\websocket.js:576:7)
[... lines matching original stack trace ...]
at TLSSocket.Readable.push (_stream_readable.js:212:10)
The code for the client and server came from here:
https://github.com/websockets/ws#Simple server
https://github.com/websockets/ws#sending-and-receiving-text-data
with ws://www.host.com/path changed to ws://<mydomainname>/<dir1>/<dir2>
Both client and server code runs properly when run locally. Http related code runs properly when used on my webhost. I am trying to get the client code to run locally and connect via websockets to my server hosted at Namecheap.
I suspect something related to the server is preventing connection. I'd ask support but I doubt if they can resolve this. Maybe there's a locked feature or something? Is there something else I could have missed?
Websockets cannot work on Namecheap's hosted servers. Their incoming ports are blocked and cannot be opened for security reasons per their customer's support.
If anyone is looking for a solution just in case, try out a Heroku free account.

unable to run node js continuously on dedicated windows server

I have a Dedicated windows server, I am running my node express code on the server, but when an exception occurred like
error: remaining connection slots are reserved for non-replication superuser connections
at Connection.parseE (C:\node\zykrrapi\prod\myapp\node_modules\pg\lib\connection.js:554:11)
at Connection.parseMessage (C:\node\zykrrapi\prod\myapp\node_modules\pg\lib\connection.js:381:17)
at TLSSocket.<anonymous> (C:\node\zykrrapi\prod\myapp\node_modules\pg\lib\connection.js:117:22)
at TLSSocket.emit (events.js:180:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at TLSSocket.Readable.push (_stream_readable.js:213:10)
at TLSWrap.onread (net.js:578:20)
Emitted 'error' event at:
at Connection.<anonymous> (C:\node\zykrrapi\prod\myapp\node_modules\pg\lib\client.js:183:19)
at Connection.emit (events.js:180:13)
at TLSSocket.<anonymous> (C:\node\zykrrapi\prod\myapp\node_modules\pg\lib\connection.js:121:12)
at TLSSocket.emit (events.js:180:13)
[... lines matching original stack trace ...]
at TLSWrap.onread (net.js:578:20)
and another error like "too many connections to the user on database" error also occurs and stops the execution of the code. this makes a huge loss of the data for me.
I want to run the Node Js Code continuously and to start the execution again if it stopped unexpectedly.
I am running the code using command "npm start" in windows server...
I want this node code to run only on the windows dedicated server.

nodejs TLSSocket.onHangUp

I have 2 nodejs modules that communicate via HTTPS.
In very rare occasions, one of the requests to the server module returns:
Stack Trace
Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1049:19)
at TLSSocket.g (events.js:260:16)
at emitNone (events.js:72:20)
at TLSSocket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:913:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickDomainCallback (node.js:397:17)
with errno set to ECONNRESET
Is there any reason why something like that would happen?
I already checked for:
Server crashes before responding
Server response takes too long
certificate expired.
Also, the server logs do not show any kind of problem.
Thanks
ECONNRESET (Connection reset by peer): A connection was forcibly closed by a peer. This normally results from a loss of the connection on the remote socket due to a timeout or reboot. Commonly encountered via the http and net modules.
"Common System Errors" : https://nodejs.org/api/errors.html
so try first to see what's happen
process.on("uncaughtException", function (err) {
console.log('err uncaught Exception : ', err);
})

Mongoose Can't Connect Without Internet

I have my MongoDB server running on localhost:27017, and while I can usually run my Node.js app fine, when I disconnect from the internet Mongoose throws the error
Error: failed to connect to [localhost:27017]
Note that I can still connect to the MongoDB server from the Mongo shell client. Also, if I start up my app first and then lose internet connection, my app can access the database fine offline. So why can't it start up without internet?
EDIT: here is the error in full
events.js:85
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (<My App>\node_modules\mongoose\
node_modules\mongodb\lib\mongodb\connection\server.js:555:74)
at emit (events.js:118:17)
at null.<anonymous> (<My App>\node_modules\mongoose\
node_modules\mongodb\lib\mongodb\connection\connection_pool.js:156:15)
at emit (events.js:110:17)
at Socket.<anonymous> (<My App>\node_modules\mongoos
e\node_modules\mongodb\lib\mongodb\connection\connection.js:534:10)
at Socket.emit (events.js:107:17)
at net.js:923:16
at process._tickCallback (node.js:355:11)
[nodemon] app crashed - waiting for file changes before starting...
Edit: wording
Use 127.0.0.1 instead of localhost. By turning off your wifi interface the OS is no longer able to resolve localhost.
Consider node-offline-localhost.
Add the following before the breaking code:
require('node-offline-localhost').always();
And it just works (hopefully), at least until RFC 3493 gets fixed.
Full disclosure: I authored this package to streamline https in my dev environment when offline.

Resources