错误:运行Node.js脚本时侦听EADDRINUSE

Error: listen EADDRINUSE when running a Node.js script

本文关键字:EADDRINUSE 脚本 js 运行 Node 错误      更新时间:2023-09-26

当我运行以下simpleJSON.js脚本时,我得到了这个Error: listen EADDRINUSE

var http = require('http'),
    fs = require('fs');
function handle_incoming_request(req, res) {
    console.log("Incoming request: (" + req.method + ") " + req.url);
    load_album_list(function(err, albums) {
        if (err != null) {
            res.writeHead(503, { "Content-Type" : "application/json" });
            res.end(JSON.stringify({ error: "file_error", message: err.message }) + "'n");
            return;
        }
        res.writeHead(200, { "Content-Type" : "application/json" });
        res.end(JSON.stringify({ error: null, data: { albums: albums }}) + "'n");
    });
}
function load_album_list (callback) {
    fs.readdir('albums/', function (err, file_list) {
        callback(err, file_list);
    });
}
var s = http.createServer(handle_incoming_request);
s.listen(8080);  

这是我在我的终端中看到的完整错误:

$ node simpleJSON.js 
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/max/dev/livelessons/photoApp/simpleJSON.js:26:3)
    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)

这是我的目录结构:

$ tree
.
├── albums
│   ├── australia2012
│   │   ├── picture_01.jpg
│   │   ├── picture_02.jpg
│   │   ├── picture_03.jpg
│   │   ├── picture_04.jpg
│   │   ├── picture_05.jpg
│   │   ├── picture_06.jpg
│   │   └── picture_07.jpg
│   ├── italy2012
│   │   ├── picture_01.jpg
│   │   ├── picture_02.jpg
│   │   ├── picture_03.jpg
│   │   ├── picture_04.jpg
│   │   ├── picture_05.jpg
│   │   ├── picture_06.jpg
│   │   └── picture_07.jpg
│   └── japan2010
│       ├── picture_01.jpg
│       ├── picture_02.jpg
│       ├── picture_03.jpg
│       ├── picture_04.jpg
│       ├── picture_05.jpg
│       ├── picture_06.jpg
│       └── picture_07.jpg
└── simpleJSON.js
4 directories, 22 files

以前用过吗?可能仍然存在连接到端口8080的剩余进程。请尝试更改端口号,看看这是否是问题所在。

终止8080端口的运行进程并重新启动节点服务器。这是因为您的8080端口在其他进程中已经很忙了。