0

I try to run my Express server on ports 80 and 443, but I'm getting an error that they are already being used.

EADDRINUSE err

However, my Ubuntu server says that in fact ports 80 and 443 aren't busy:

ubuntu@ip-182-47-78-432:~$ sudo netstat -nlp

I get:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      4549/node
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1102/sshd
tcp        0      0 0.0.0.0:27000           0.0.0.0:*               LISTEN      14651/mongod
tcp        0      0 0.0.0.0:8090            0.0.0.0:*               LISTEN      4549/node
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      12374/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      1102/sshd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           580/dhclient
udp        0      0 0.0.0.0:23728           0.0.0.0:*                           580/dhclient
udp6       0      0 :::1742                 :::*                                580/dhclient
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     8966     1104/acpid          /var/run/acpid.socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     7449     412/systemd-udevd   /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     8521     869/dbus-daemon     /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     7430879  14651/mongod        /tmp/mongodb-27000.sock
unix  2      [ ACC ]     STREAM     LISTENING     1636971  1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     19618017 20335/node          /home/ubuntu/.forever/sock/worker.1429234248700JdV.sock
unix  2      [ ACC ]     STREAM     LISTENING     8616395  12374/mongod        /tmp/mongodb-27017.sock
unix  2      [ ACC ]     STREAM     LISTENING     19620699 21172/node          /home/ubuntu/.forever/sock/worker.1429234407027ZZT.sock

What can be happening? Or what other command can I run to find something useful?


Edit 1:

My code:

var fs = require('fs');
var http = require('http');
var https = require('https');
var app = require('./app');

var credentials = {
    key: fs.readFileSync('private_key.pem', 'utf8'),
    cert: fs.readFileSync('com_certificate.pem', 'utf8'),
    ca: [
        fs.readFileSync('com_certificate_chain_1.pem', 'utf8'),
        fs.readFileSync('com_certificate_chain_2.pem', 'utf8')
    ]
};

http.createServer(app).listen(80, function() {
    console.log('HTTP server started on port ' + 80 + '...');   
});

https.createServer(credentials, app).listen(443, function() {
    console.log('HTTPS server started on port ' + 443 + '...');
});

Edit 2:

I'm on an Amazon cloud server, it doesn't have a Skype running.

Edit 3:

var express = require('express');
var app = module.exports = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var methodOverride = require('method-override');
var cors = require('cors');
var corsOptions = require('./config/cors');
var config = require('./config/db');
var spreadsheet = require('./services/spreadsheet');

// models
var User = require('./app/models/user.js');
var Goal = require('./app/models/goal.js');
var Portfolio = require('./app/models/portfolio.js');
var Security = require('./app/models/security.js');

// environment variables
app.set('views', __dirname + '/app/views');
app.set('view engine', 'ejs');

// configure db
app.set('/api/v1', config.db[app.settings.env]);
mongoose.connect(app.get('/api/v1'), function(err) {
    if(err) { return console.error(err); }
});

// mongoose.connection.on('error', console.error.bind(console, 'Connection error: '));
mongoose.connection.once('open', function cb() {
    console.log('Connected.');
    console.log(app.settings.env);
});

// get data from a POST request
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// get data from cookies
app.use(cookieParser());

// make PUT and DELETE requests from views
app.use(methodOverride(function(req, res) {
    if (req.body && typeof req.body === 'object' && '_method' in req.body) {
        var method = req.body._method;
        delete req.body._method;
        return method;
    }
}));

// enable pre-flight and cors for every route
app.options('*', cors());
app.use(cors(corsOptions));

// force HTTPS redirect
app.use(function(req, res, next) {
    if (!req.secure) {
        return res.redirect('https://' + req.get('host') + req.url);
    }
    next();
});

// api routes
var user_routes = require('./routes/user_routes');
var security_routes = require('./routes/security_routes');
var portfolio_routes = require('./routes/portfolio_routes');
var goal_routes = require('./routes/goal_routes');
app.use('/api/v1/users', user_routes);
app.use('/api/v1/securities', security_routes);
app.use('/api/v1/portfolios', portfolio_routes);
app.use('/api/v1/goals', goal_routes);

// MVC routes
var user_controller = require('./app/controllers/user_controller');
var security_controller = require('./app/controllers/security_controller');
var portfolio_controller = require('./app/controllers/portfolio_controller');
var goal_controller = require('./app/controllers/goal_controller');
app.use('/users', user_controller);
app.use('/securities', security_controller);
app.use('/portfolios', portfolio_controller);
app.use('/goals', goal_controller);

Edit 4:

The error message I get when trying to run the server:

ubuntu@ip-172-31-15-213:~/services/api$ nodemon server.js
17 Apr 21:12:17 - [nodemon] v1.3.7
17 Apr 21:12:17 - [nodemon] to restart at any time, enter `rs`
17 Apr 21:12:17 - [nodemon] watching: *.*
17 Apr 21:12:17 - [nodemon] starting `node server.js`

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/ubuntu/services/api/server.js:17:24)
    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)
17 Apr 21:12:17 - [nodemon] app crashed - waiting for file changes before starting...

Looks like I got a different error this time though...

Edit 5:

If I run telnet localhost 80, I get:

ubuntu@ip-172-31-15-213:~/services/api$ telnet localhost 80
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
bmpasini
  • 1,503
  • 1
  • 23
  • 43

2 Answers2

1

Your error says Error: listen EACCES, not EADDRINUSE.

The reason for the EACCES is that binding to ports below 1024 (like 80 and 443) requires root privileges, and you don't seem to be starting your app as root (before doing that, please make sure you know the implications of running apps as the root user).

robertklep
  • 198,204
  • 35
  • 394
  • 381
-1

If you are using skype

try disabling it to use port 80 for connections

Open Skype if it has not already been launched
Go to Tools –> Options from the dropdown menu
Select “Advanced” in the left-hand column, last option
Select “Connection”
Deselect the option that says, “Use port 80 and 443 as alternatives for incoming     connections”
Click Save
Exit and then restart Skype

here is the link

Rohit Rehan
  • 568
  • 1
  • 4
  • 18