2

I am new to nodejs. Started learning with this simple program .

var http=require("http");
var host="127.0.0.1";
var port=10016;

var server=http.createServer(function(request, responce) {
  console.log("Request recieved : " + request.url);
  responce.writeHead(200, {"Content-type":"text/plain"});
  responce.write("Hello World.! This is the start of my journey in nodejs");
  responce.end();
});

server.listen(host, port, function() {
  console.log("Listening "+ host +":" + port)});

When I run the program using "node server.js " I encounter this error which , although I surfed, I could not find any relevant explanations apart from using different ports. I tried as many as any . I am a windows user. Mac users might have sudo with root permissions, but the problem isnt the same. Please help. Thanks in advance.

My error is : C:\node>node firstserver.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:1132:5)
at Object.<anonymous> (C:\node\firstserver.js:12: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)

Help appreciated.!

raina77ow
  • 103,633
  • 15
  • 192
  • 229
Spandana Jami
  • 117
  • 2
  • 2
  • 11

1 Answers1

2

Just swap the server.listen arguments in your code - it should be written like this:

server.listen(port, host, function() {
  console.log("Listening "+ host +":" + port)});

port is the first argument, because the second one is optional. Quoting the docs:

server.listen(port, [hostname], [backlog], [callback])

Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).

Community
  • 1
  • 1
raina77ow
  • 103,633
  • 15
  • 192
  • 229