0
votes

Pardon my n00biness here. I've set up a simple website app through AngularJS and NodeJS on Ubuntu 14.04. After starting my server, the IP address doesn't load and throws a connection failure error.

  • I've been able to successfully connect to the Ubuntu server on AWS

  • successfully installed npm, node, and pulled git repo

  • tried launching the server by entering "sudo nodejs server.js" in API folder

  • server launches, but IP address doesn't load project .. no errors in console..

These were my steps in Terminal from start to finish after logging into my SSH server:

ubuntu@ip-172-31-25-241:~$ sudo mkdir src
ubuntu@ip-172-31-25-241:~$ cd src
ubuntu@ip-172-31-25-241:~/src$ sudo git init (after installing git)
ubuntu@ip-172-31-25-241:~/src$ sudo git pull origin master (pulled repo)
ubuntu@ip-172-31-25-241:~/src$ sudo apt-get install npm
ubuntu@ip-172-31-25-241:~/src$ sudo apt-get install nodejs
ubuntu@ip-172-31-25-241:~/src$ cd API
ubuntu@ip-172-31-25-241:~/src/API$ ls
server.js
ubuntu@ip-172-31-25-241:~/src/API$ sudo nodejs server.js
Server Started on http://localhost:8080
Press CTRL + C to stop server

Here is my code from my server.js file as requested by @JoKeRxbLaCk:

// initialize Express
var express = require('express');
// run Express
var app = express();

app.use(express.static(__dirname + './../APP'));

app.get('/', function(req, res) {
    res.send('Express is running!');
});


app.listen(80, function() {
    console.log('Server Started on http://localhost:8080');
    console.log('Press CTRL + C to stop server');
});

When I try the command "nodejs server.js" instead of "sudo nodejs server.js", I get this error:

ubuntu@ip-172-31-25-241:~/src/API$ nodejs server.js

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 EventEmitter.listen (/home/ubuntu/src/node_modules/express/lib/application.js:617:24)
    at Object.<anonymous> (/home/ubuntu/src/API/server.js:15: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 expect the server to not throw an error and for my site to load :( Please help! I know I'm missing a step but can't figure out where.

2
Can you share the code in server.js? Let's try to start the node js process without sudo, so: nodejs server.js.JoKeRxbLaCk
@JoKeRxbLaCk added the codeshway
The url you are trying to access is http://localhost:8080 or http://localhost:80 ? Because you are setting in express to listen on port 80, do you have an nginx or apache that redirect from port 8080 on port 80?JoKeRxbLaCk
@JoKeRxbLaCk I've tried accessing both of those but don't see anything, same with the IP address of my running instance. I've basically copied the code my other existing website, which works perfectly fine with this code. I can't figure out which step I'm missing..shway
@shway try what I said in my answerAbhishek Ranjan

2 Answers

0
votes

You are trying to run your application on port 80. On Linux, ports < 1024 are privileged ports and require raised privileges to run.

Your application will therefore run as sudo because you are running with elevated privileges. Without the sudo, you do not have permission to use port 80.

So you either need to make sure that the user running your app has the correct privileges or front your app with something like Apache or nginx that runs on port 80 and forwards to your app on a non-privileged port (e.g. 8080).

-1
votes

On AWS you have to manually open any ports you want to use.
Here's a guide for that Open Port on AWS Ec2