简体   繁体   中英

node app.js is not working on express 4?

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);

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

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.

我遇到了同样的问题,我在Ubuntu上,我刚刚将端口号更改为3000,它解决了该问题。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM