Running simple node js server - node.js

I am running a simple node js server on my Mac. I also have mamp installed. Just FYI.I am able to go to localhost:8888 just fine and preview the web page. However, in my server.js file, if I include the host as a parameter to the server.listen function like so
server.listen(127.0.0.1, 8888, function(){
console.log('Server running');
})
I get the following error in my terminal.
Error: listen EADDRINUSE 127.0.0.1
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1338:19)
at listenInCluster (net.js:1396:12)
at Server.listen (net.js:1491:5)
at Object.<anonymous> (/Users/username/Desktop/nodeServer/server.js:12:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
Users-Mac-Pro:nodeServer user$
If I just have
server.listen(8888, function(){
console.log('Server running');
})
everything works fine. Does this have anything to do with the fact that I have mamp installed? Just want to understand why one works and the other doesn't.

Try putting your host after port as following
server.listen(8888, '127.0.0.1', function() {
console.log('Server running');
})
app.listen documentation
app.listen([port[, host[, backlog]]][, callback])

Some other process might have been using 8888 port. You can either kill that process or use someother port for your nodejs server.
To get the process id of the process using port 8888, use
lsof -i tcp:8888
and kill the process using
kill -9 <pid>
Hope this helps.

Probably you're currently running your server in that port. Try with:
killall node

Related

why node js socket not working when change port to https?

Normally in app.js i use this code on port 3000 it's work good (on my-domain.com:3000).
.
http.listen(3000, function(){
console.log('start server on port :3000');
});
then i want to use on https, so i change app.js to
http.listen(443, function(){
console.log('start server on port :443');
});
When run node app.js , it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::443
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:28:6)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
Normally i have to access to my-domain.com:3000 for user chat. So i want to know how can i access to https://www.my-domain.com for user chat ?
You have the following error message:
Error: listen EADDRINUSE :::443
[...]
This message means that the port 443 is currently in use by some process.
You can check which process is actually using the said port by executing one of the many tools for network checking (such as netstat for Windows, lsof or netstat on Linux).
Refer to the manual for those tools to achieve the correct result, based upon your operating system.

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);
});

HTTPs port error on NodeJs and Express, why? banging my head on wall

Why do I get an error on my Amazon server when running my NodeJS app?
sudo node app.js
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (/home/ubuntu/www/app.js:65:38)
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)
Here is my code:
var app = express();
var server = http.createServer(app).listen(4000, function() {
console.log('Express HTTP server listening on port ' + app.get('port'));
});
https.createServer(credentials, app).listen(443, function() {
console.log('Express HTTPS server listening on port 443');
});
I think the problem is the 443 because when I delete the https.createServer "listen()" connection then everything works fine.
Thanks for your attention :)
You probably have another process running that's using this port (probably another instance of your app.
Use ps aux to see your running processes.
Use lsof -i | grep 4000 to see what's using this port.

EADDRINUSE with Node.js on OpenShift

Writing a test application in Node.js running on OpenShift and it currently will not start.
This is my code:
#!/bin/env node
var http = require('http');
var ip = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(ip, port);
console.log('Server running');
I get the error
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1003:19)
at listen (net.js:1044:10)
at Server.listen (net.js:1104:5)
at Object.<anonymous> (/var/lib/openshift/52854c6f4382ec071400051d/app-root/runtime/repo/server.js:11: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)
DEBUG: Program node server.js exited with code 8
Which seems to mean I can't bind to the port. I've found various information on why this might be but none of it seems relevant - it all seems related to permissions but surely the point of process.env.OPENSHIFT_NODEJS_* is I have permission to bind to it?
It means some other process is using your port process.env.OPENSHIFT_NODEJS_PORT. EACCESS would mean permission denied, not EADDRINUSE. Try changing to unused ports like 8080. You can check used ports with :
netstat -tulpn
netstat -tulpn | grep :$OPENSHIFT_NODEJS_PORT //filter by your 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