How to make NodeJS Server deployed on AWS cloud go live? - node.js

I am new to NodeJS.
I have the following code for hello world.
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World!\n I am the AWS cloud');
}).listen(8081);
console.log('Server is running at http://127.0.0.1:8081');
This code is saved on my AWS cloud which runs an Ubuntu 14.04 instance.
I have installed nodeJS on Ubuntu.
When I run the code it works fine and displays the console log message, but when I open a web browser on a different machine and type http://Public-IP-Address:8081 it does not work.
Also when I stop the script by pressing Ctrl+c and then again execute it, it displays the following error message:
Server is running at http://127.0.0.1:8081
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1156:14)
at listen (net.js:1182:10)
at Server.listen (net.js:1267:5)
at Object.<anonymous> (/home/ubuntu/NodeJSscripts/hw.js:17:22)
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)
When I go back and change the port number and run the script again, it then runs again without throwing any error, but the web browser still doesn't connect to the server through it's public IP.

Related

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

running basic http server using node.js on Mac

I'm new to node.js and was trying to run the following training example for setting up a basic http server on my mac (OS X Yosemite 10.10.3):
var http = require("http");
http.createServer(function (request, response) {
request.on("end", function () {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
});
}).listen(8080);
I get the following error when executing the code:
$ node http.js events.js:85
throw er; // Unhandled 'error' event
^ Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1156:14)
at listen (net.js:1182:10)
at Server.listen (net.js:1267:5)
at Object. (/Users/sumankalyan/WebstormProjects/Node-Server/http.js:25:4)
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)
Not sure if this is a localhost config issue or something else. Any help much appreciated.
Another application is using the port 8080. You can run
lsof -i :8080
to see what it is. You can also just try another port.

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

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

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