ECONNREFUSED error while running express code - node.js

I am not able to run the server ...... I am getting the error as ECONNREFUSED
How to resolve this Error !
When i tried to use different ports .... all are giving me the same error !
ubuntu#ip-MyIP:~/rainmelon/projects/FindMyBuffet$ node app.js
Express server listening on port 7005
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)
--------------------
at Handshake.Sequence (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
at new Handshake (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
at Protocol.handshake (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/Protocol.js:42:50)
at Connection.connect (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/Connection.js:73:18)
at Object.<anonymous> (/home/ubuntu/rainmelon/projects/FindMyBuffet/app.js:15:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

The traceback says where the exception is coming from:
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)
--------------------
...
at Connection.connect (.../node_modules/mysql/lib/Connection.js:73:18)
--> ^^^^^
at Object.<anonymous> (/home/ubuntu/rainmelon/projects/FindMyBuffet/app.js:15:12)
--> ^^^^^^^^^
So your app can't connect to MySQL.
This is usually down to either incorrect hostname/portname in your MySQL driver configuration, the MySQL server not running, or that your MySQL server isn't configured to listen on TCP sockets. See here.

Your mysql process is down, which means it is not running. You need to restart your mysql process (changing ports wont help).
To solve this problem you need to restart it. You can do by doing any of the following:
You can start your wamp or xamp server which will automatically start the process.
Or you can open the commandline prompt and start it manually like so "c:\wamp\bin\mysql\mysql5.5.24\bin\mysqld.exe"
Note that using the second method will require knowing the exact location of your wamp folder like I have used mine up top.(in quotes)

You may do a netstat to find out the pid of process running on the port 7005 and then go for a forceful kill with the pid got.
like
netstat -plten |grep 7005
kill -9 16085
where 16085 is the pid obtained from prev command. And restart the express app.
reference
How to kill a process running on particular port in Linux?

Related

error that wont go away despite numerous changes in node.js

Good evening,
I have the following error and I have no idea what it means, i have googled and changed a few little bit but its very persistent. Is anyone able to offer me some help with the matter?
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Object.<anonymous> (/Users/danielwilstrop/Desktop/projects/live-chat-2/server.js:100:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
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: -48,
syscall: 'listen',
address: '::',
port: 5000
}
[nodemon] app crashed - waiting for file changes before starting...
This error is usually caused by a typo on your terminal.
Check if you typed the correct file name in your terminal and also check if the path to your file is correct.
Option 2: check if no other terminals are open in the background. kill all those other terminals, press Ctr+C then run your app again..
There is some other process already using port 5000 so, you are getting this error. To solve this there are two ways
Change the server port from 5000 to something else which port is available eg. 5555 in this file /Users/danielwilstrop/Desktop/projects/live-chat-2/server.js at line number 100 (as shown in the error)
Kill the process that is running on port 5000. Then run the server.js file.
For Linux or mac sudo lsof -ti tcp:5000 | sudo xargs kill
For windows run cmd with administrator privilages netstat -ano|findstr "PID :5000" take the process id from the output and then taskkill /pid <PROCESS_PID_USING_PORT_5000> /f

Error: listen EADDRINUSE: address already in use :::8080 while clicking on run script

Firstly I run this command (npm ndb server.js) then a debugging chrome window appear and after this
Actually, I got these error when I am trying to debug in my code and I clicked on run script after that a debugging chrome window appear then it give these wired error.
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::8080
at Server.setupListenHandle [as _listen2] (net.js:1279:14)
at listenInCluster (net.js:1327:12)
at Server.listen (net.js:1414:7)
at Function.listen (C:\Users\Abhishek kumar\natours\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Abhishek kumar\natours\server.js:24:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1306:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
The error is quite explicit :
Error: listen EADDRINUSE: address already in use :::8080
means that some program is already listening on port 8080 (and you have an extra hint : the ::: says that it's listening on broadcast host using IPv6).
If you're curious what program is listening on that port, you could try to navigate to http://localhost:8080 and see what's displayed. If nothing is displayed then it's not one of your web apps. Maybe you have an HTTP proxy running on your machine (I have recently come across a malware that ran mitmproxy on target machines on port 8080 to intercept all traffic).
On Mac or Linux you can use lsof to get more information on the program listening on that port.
The command you want is :
$ sudo lsof -i :8080
Run this command.
killall node

Nodejs with Forever script will not accessible after sometime

I got a Nodejs project with was hosted in Digital Ocean and I keep it running using Forever script. But I realized that there will be some error after a period of time which causing the site down. It's not just happened once but few times already, below is the Forever log:
Error: Connection lost: The server closed the connection.
at Protocol.end (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:109:13)
at Socket.<anonymous> (/var/www/menu/node_modules/mysql/lib/Connection.js:109:28)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)
error: Forever detected script exited with code: 1
error: Script restart attempt #178
Listening on xxx.xxx.xxx.xxx, server_port 80
events.js:141
throw er; // Unhandled 'error' event
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
--------------------
at Protocol._enqueue (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/var/www/menu/node_modules/mysql/lib/Connection.js:130:18)
at Connection._implyConnect (/var/www/menu/node_modules/mysql/lib/Connection.js:461:10)
at Connection.query (/var/www/menu/node_modules/mysql/lib/Connection.js:206:8)
at Object.<anonymous> (/var/www/menu/config/db.js:14:12)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
error: Forever detected script exited with code: 1
I'm not sure why this is happening as the log did not stated which line was causing error, and it seems like Forever had try to restart the script but it failed. But once I SSH into my server and start it again then it's working fine already. Anyone able to help? Thanks.
Error: connect ECONNREFUSED 127.0.0.1:3306
You are getting this error that means that port is in use. You can kill the process running over that port by using following command.
sudo kill $(sudo lsof -t -i:3306)
Thereafter, you can re-deploy your server using forever like you do.
Happens to me once
The problem seems to be related to mysql connection.
Please refer to this stackoverflow question to solve your problem
nodejs mysql Error: Connection lost The server closed the connection

Running sockets.io in node without sudo access

I have a socket.io node file that starts like this...
//sockets/server
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(80); //the 5th line that the error refers to
When I run from the console with "node myFile.js" I get the following error...
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1112:19)
at listen (net.js:1155:10)
at Server.listen (net.js:1240:5)
at Object.<anonymous> (/home/myUser/node/myFile.js:5:5)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
I can easily get around the error on my local machine (MAMP) by adding 'sudo' to the start of the command, but my hosting provider won't let me do that on the production environment.
Does anyone have any idea of a way to fix the error (or even know what it means) as opposed to sudoing it out of the way?
Port 80 is a privileged port, and you need root or CAP_NET_ADMIN to bind to it.
The solutions are:
Do not bind to port 80 for development.
Configure your system to allow the port to be used as a normal user. See: https://superuser.com/questions/710253/allow-non-root-process-to-bind-to-port-80-and-443

Writing a node application and encountering an error when running 'node server.js'

I'm writing a node application and I recently switched from port 3000 to port 80 on my Mac Os X Lion machine (running 10.7.4) and whenever I run node server.js I get the following error
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:768:11)
at HTTPServer.Server._listen2 (net.js:891:19)
at listen (net.js:935:10)
at HTTPServer.Server.listen (net.js:984:5)
at Object.<anonymous> (/Users/ajain/Documents/Projects/Time-Feed/server.js:127:5)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Any idea on what I have to do to fix the issue?
You need to have the root privilege in order to listen on a port number below 1024. Therefore, you may use the sudo command.
sudo node server.js
If sudo didn't help just change the port to something like 1234 or maybe 2000 or 3000.

Resources