33

I just installed node.js on Windows. I have this simple code which does not run:

I get: Error: listen EADDRINUSE

Is there a config file that tells node.js to listen on a specific port?

The problem is I have Apache listening on port 80 already.

EDIT:

var http = require('http'); 
var url = require('url'); 

http.createServer(function (req, res) { 
 console.log("Request: " + req.method + " to " + req.url); 
 res.writeHead(200, "OK"); 
 res.write("<h1>Hello</h1>Node.js is working"); 
 res.end(); 
}).listen(5454); 
console.log("Ready on port 5454");
CD..
  • 72,281
  • 25
  • 154
  • 163
jim dif
  • 641
  • 1
  • 8
  • 17
  • var http = require('http'); var url = require('url'); http.createServer(function (req, res) { console.log("Request: " + req.method + " to " + req.url); res.writeHead(200, "OK"); res.write("

    Hello

    Node.js is working"); res.end(); }).listen(5454); console.log("Ready on port 5454");
    – jim dif Aug 29 '12 at 15:24

3 Answers3

32

There is no config file unless you create one yourself. However, the port is a parameter of the listen() function. For example, to listen on port 8124:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

If you're having problems finding a port that's open, you can go to the command line and type:

netstat -ano

To see a list of all ports in use per adapter.

Community
  • 1
  • 1
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • The tut I am following says to first go to a DOS command line and enter node t.js t.js being the name of my test file. So the error is in the command line window. – jim dif Aug 29 '12 at 15:32
  • Which port are you trying to listen on? Try a few common ones, such as `81` and `8080`.. Do you still get the EADDRINUSE error? – Mike Christensen Aug 29 '12 at 15:38
  • Mike thanks. I used the code above with 8124 as the port but the key was to put my test file in the same directory as nodejs and to be in the same directory with respect to the dos prompt. – jim dif Aug 29 '12 at 15:48
  • i added these lines in the end of app.js, but it keeps listening to port 1337 – Vignesh Subramanian Aug 17 '15 at 06:50
10

I usually manually set the port that I am listening on in the app.js file (assuming you are using express.js

var server = app.listen(8080, function() {
    console.log('Ready on port %d', server.address().port);
});

This will log Ready on port 8080 to your console.

maudulus
  • 10,627
  • 10
  • 78
  • 117
3

you can get the nodejs configuration from http://nodejs.org/
The important thing you need to keep in your mind is about its configuration in file app.js which consists of port number host and other settings these are settings working for me

backendSettings = {
"scheme":"https / http ",
"host":"Your website url",
"port":49165, //port number 
'sslKeyPath': 'Path for key',
'sslCertPath': 'path for SSL certificate',
'sslCAPath': '',
"resource":"/socket.io",
"baseAuthPath": '/nodejs/',
"publishUrl":"publish",
"serviceKey":"",
"backend":{
"port":443,
"scheme": 'https / http', //whatever is your website scheme
"host":"host name",
"messagePath":"/nodejs/message/"},
"clientsCanWriteToChannels":false,
"clientsCanWriteToClients":false,
"extensions":"",
"debug":false,
"addUserToChannelUrl": 'user/channel/add/:channel/:uid',
"publishMessageToContentChannelUrl": 'content/token/message',
"transports":["websocket",
"flashsocket",
"htmlfile",
"xhr-polling",
"jsonp-polling"],
"jsMinification":true,
"jsEtag":true,
"logLevel":1};

In this if you are getting "Error: listen EADDRINUSE" then please change the port number i.e, here I am using "49165" so you can use other port such as 49170 or some other port. For this you can refer to the following article
http://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-shared-hosting-accounts

Gaurav Singh
  • 189
  • 2
  • 7