0

I have this code in a file named server.js:

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

io.on('connection', function(){
    console.log("a client connected");  
});

server.listen(80);

When i run node server.js in the terminal (cmd), it throws this error:

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> (C:\inetpub\wwwroot\socketio\server.js:9:8)
    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)

Do you have an idea of what could be the problem? Thanks!
Note: I already have socket.io and express installed.

2 Answers 2

1

It seems you are not allowed to open a serversocket on port 80. See this line in the error message

C:\inetpub\wwwroot\socketio\server.js:9:8

Socket addresses smaller than 1024 are reserved for system use. Try this:

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

io.on('connection', function(){
    console.log("a client connected");  
});

server.listen(3000);

Sign up to request clarification or add additional context in comments.

2 Comments

Yup. I discovered that when I tried to change the port to 8000. Thanks for you answer!
You can find more information on why port 0-1024 are restricted here: unix.stackexchange.com/questions/16564/…
0

Looks like the port 80, is invalid or unacceptable. I tried to change it to another port (8000) and it worked.

server.listen(8000);

The exception is gone. I hope I can find a way to distinguish between valid/invalid ports at run-time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.