Writing a node application and encountering an error when running 'node server.js' - node.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.

Related

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

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

port 100 not running in node.js in localhost

I have created a http server in node.js with the following code, and trying to run it on port 100:
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("Howdy");
}).listen(100);
console.log("server running on port 100");
With this, the server does not start and I am getting the following error message on the linux console:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Object.<anonymous> (/home/badhai/Desktop/mainn.js:6:4)
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)
But if I lift a sails.js app on port 100, it runs successfully on port 100. However, the above code runs successfully on port 8081. I want to know if I need to make any changes in the server creation method or elsewhere so that it can be made to run successfully on port 100?
The EACCES part of the error message is the key here - it means you don't have access to that port. Ports < 1024 are system reserved. It's better to use ports in the range 1024-65535
Most modern operating systems limit binding to reserved ports (less than 1024) to processes running as root (or equivalent).
If you absolutely must bind to port 100, googling around will give you a bunch of ways to do it:
https://gist.github.com/firstdoit/6389682
It's better to use ports > 1024. I'm using ports starting from 3000. But if you really want to start it on port 100 and you understand what you're doing then install setcap and just allow to bind ports <1024.
> sudo apt-get install setcap
> sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node
NodeJS can be installed in other directory also, so better to check where it is and call above setcap command with your path.
> which node
/usr/local/bin/node

New to using NodeJS Express Server

I trying to view a static html page at localhost, but get the following error message:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Function.app.listen (/home/phil/xProgramming/AngJS/Project/node_modules/express/lib/application.js:536:24)
at Object.<anonymous> (/home/phil/xProgramming/AngJS/Project/server.js:6:5)
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)
I've tried killall, but I still get the same error message.
I new to Linux and am using Ubuntu 14.04
The EACCES error normally means that you are trying to bind to a port and failing. This is probably because you are trying to bind to port 80 without root permissions. Try running node with root permissions and it should work.
sudo node server.js
or
sudo nodejs server.js
Depending on how you have Ubuntu setup.
If this is a development environment instead of running as root you should use a port greater than 1024. Typically you will find people using port 3000 for node though 8000 and 8080 are also used. Here is how I normally do this when using Express.
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});

ECONNREFUSED error while running express code

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?

Resources