0

I am following this tutorials to experiment Exress 4 router.

But when I type: node server.js, I got the fllowing error:

> events.js:72
>         throw er; // Unhandled 'error' event
>               ^ Error: listen EADDRINUSE
>     at errnoException (net.js:901:11)
>     at Server._listen2 (net.js:1039:14)
>     at listen (net.js:1061:10)
>     at Server.listen (net.js:1135:5)
>     at Function.app.listen (/Users/tom/Documents/Projects/express4router/node_modules/express/lib/application.js:531:24)
>     at Object.<anonymous> (/Users/tom/Documents/Projects/express4router/server.js:22:5)
>     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)

It looks like node app.js doesn't work in express4. How do I run this simple script in express 4?

var express = require('express');
var app     = express();
var port    =   process.env.PORT || 8080;

app.get('/sample', function(req, res) {
    res.send('this is a sample!');  
});

app.listen(port);
console.log('Magic happens on port ' + port);
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
Nicolas S.Xu
  • 13,794
  • 31
  • 84
  • 129

4 Answers4

2

EADDRINUSE means that you have something else listening on that port, more than likely another node app.

You can use lsof to determine what process is running on that port, if you are uncertain

Community
  • 1
  • 1
Patrick
  • 13,872
  • 5
  • 35
  • 53
  • But "lsof -i :8080" does not show any result, no output at all. 8080 is previous port number which is supposed to be occupied. Did use the right arguments for lsof? – Nicolas S.Xu May 03 '14 at 07:13
1

EADDRINUSE says that the port is already in use. Probably you already started your app once before and it is still running. (Happened to me sometimes too when I migrated my express app.) Check if there are instances running and terminate those. If there are none try another port. (Maybe there is an completely unrelated application running on your pc that is using that port.)

Your code looks good btw.

analog-nico
  • 2,750
  • 17
  • 25
0

I had the same problem, I am on Ubuntu, I have just changed the port number to 3000 and it fixed the problem.

mhatch
  • 4,441
  • 6
  • 36
  • 62
Bouchra
  • 51
  • 5
0

You want to use port 8080 for your application, but that is already being utilized by some other application. That's why you are getting this error "EADDRINUSE."

> events.js:72
>         throw er; // Unhandled 'error' event
>               ^ Error: listen EADDRINUSE

So for it, Either changes the port number in your code line 3 from 8080 to some other free port between 1024-65535 or transfer the program running on port 8080 to some other port. Or you can forcefully kill the program if that is not more useful for you.

Use this command to list all the ports those are being utilized currently on your system

$ netstat -plten // or netstat -p tcp -ano

For your program to identify which program is utilizing port 8080. You can run this command

$ netstat -plten | grep 8080 // or netstat -p tcp -ano | grep 8080

The above command will result something like this :

tcp6       0      0 :::8080                :::*                    LISTEN      2237/unknownprogram     off (0.00/0/0)

The number before /unknownprogram is a process id. Now use kill command to kill the process

$ kill -9 8080

-9 implies the process will be killed forcefully.

Rajdeep Gautam
  • 642
  • 9
  • 15