gpt4 book ai didi

javascript - 错误: listen EADDRINUSE when running a Node. js脚本

转载 作者:行者123 更新时间:2023-11-30 10:16:36 24 4
gpt4 key购买 nike

当我运行以下 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。尝试更改端口号以查看是否是问题所在。

关于javascript - 错误: listen EADDRINUSE when running a Node. js脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com