nodejs: listen EACCES: permission denied 0.0.0.0:80

Viewed 84180

I am trying to create https server to test socket io by node js. According this page

openssl genrsa -out privatekey.pem 2048 
openssl req -new -key privatekey.pem -out certrequest.csr 
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

enter image description here

i created privatekey.pem and certificate.pem and this is my codes:

var express = require('express');
var https = require('https');
var fs = require('fs');

var options = {
    key: fs.readFileSync('privatekey.pem'),
    cert: fs.readFileSync('certificate.pem')
  };
var app = express();

 var server = https.createServer(options, app).listen(443);

var io = require('socket.io').listen(server,()=>{
    console.log('listen to https');
});



io.on('connection', function (socket) {
    console.log(' user connected');
    socket.on('disconnect', function () {
        console.log('a user disconnected');
    });
});

but after running server i got this errors:

tazik@mx:/mnt/Projects/Projects/nodejs/socketserver
$ node app.js 
events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied 0.0.0.0:80
    at Server.setupListenHandle [as _listen2] (net.js:1292:21)
    at listenInCluster (net.js:1357:12)
    at Server.listen (net.js:1445:7)
    at Object.<anonymous> (/mnt/Projects/Projects/nodejs/socketserver/app.js:15:24)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1336:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EACCES',
  errno: 'EACCES',
  syscall: 'listen',
  address: '0.0.0.0',
  port: 80
}

I am using linux mx .

8 Answers

Non-privileged user (not root) can't open a listening socket on ports below 1024.

Check this

Give Safe User Permission To Use Port 80

Remember, we do NOT want to run your applications as the root user, but there is a hitch: your safe user does not have permission to use the default HTTP port (80). You goal is to be able to publish a website that visitors can use by navigating to an easy to use URL like http://ip:port/

Unfortunately, unless you sign on as root, you’ll normally have to use a URL like http://ip:port - where port number > 1024.

A lot of people get stuck here, but the solution is easy. There a few options but this is the one I like. Type the following commands:

> sudo apt-get install libcap2-bin 
> sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\`` 

Now, when you tell a Node application that you want it to run on port 80, it will not complain.

EDIT: Add a space in the setcap command

I believe you need root privileges to run on ports below 1024.

sudo node app.js

If you do not have a sudo account, try using other ports, 8080 and 4433 for example.

It's a permission issue, you can just write sudo as a prefix

sudo npm start

It worked for me!

@Sohan answer alternate

I had to use yum instead of apt-get

sudo yum install libcap2-bin 
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

In case of windows user, some other app or service is using port 80 that's why we get this error. You can check which all apps are using this service

You can stop all services which are using port 80 by using following command in your command:

net stop http

Now try to run app which you want to run on port 80.

In server.listen the first argument must be port and then hostname:

server.listen(port , hostname , () => 
    console.log('server started'); 
});

Try this,

var server = https.createServer(options, app); // Creating a server
var io = require('socket.io').listen(server); // Mapping socket with server

server.listen(8443,() => { // setting port for existing server
    console.log('Https listening'); // Now socket and server both listens to same port
});
Related