[已解决]关于Nodejs的node-inspector报错了,请指教

[已解决]关于Nodejs的node-inspector报错了,请指教

错误很简单,应该就是说端口重了,访问http://127.0.0.1:8080/debug?port=5858 控制台信息: Error: read ECONNRESET Check there is no other debugger client attached to port 5858.

查了论坛和网上的帖子,关于inspector好像没说这个错,我找了好一会不知道哪里有问题,之前还用inspector来着,今天不知道怎么就报错了

代码如下:

(function (exports, require, module, __filename, __dirname) { var http = require(‘http’); var url = require(‘url’); var fs = require(‘fs’);

http.createServer(function(req,res){ //截取请求的url,端口后后面的部分 var pathname = url.parse(req.url).pathname; var realpath = “public”+pathname;

fs.exists(realpath,function(exists){ console.log(exists); if(!exists){ res.writeHead(404,{‘Content-Type’:‘text/plain’}); res.write(“404 not found!”); res.end(); }else{ fs.readFile(realpath,‘utf-8’,function(err,data){ if(!err){ console.log(data); res.writeHead(200,{‘Content-Type’:‘text/html’}); res.write(data,‘utf-8’); res.end(); }else{ res.writeHead(500,{‘Content-Type’:‘text/plain’}); res.end(err); } }); } }); //res.write(pathname); //res.writeHead(200,{‘Content-Type’:‘text/plain’}); //res.end(); }).listen(8888) });

就是一个静态文件服务器的demo,首先node-inpector,然后新开一个cmd,node --debug app.js,这两个顺序反过来也没用,明白的请指教~


7 回复

[已解决]关于Nodejs的node-inspector报错了,请指教

问题描述

在使用 node-inspector 进行调试时遇到了端口冲突的问题。具体表现为访问 http://127.0.0.1:8080/debug?port=5858 时出现以下错误:

Error: read ECONNRESET
Check there is no other debugger client attached to port 5858.

解决方案

该错误提示是因为端口 5858 已经被其他进程占用。你需要确保没有其他调试器客户端连接到该端口。

以下是具体的解决方案:

  1. 检查并关闭占用端口的进程: 使用以下命令可以查看占用指定端口的进程,并终止它。

    lsof -i :5858
    # 或者在Windows上使用
    netstat -ano | findstr :5858
    

    找到对应的进程ID(PID),然后使用以下命令终止该进程。

    kill <PID>
    # 或者在Windows上使用
    taskkill /F /PID <PID>
    
  2. 修改Node.js应用的调试端口: 如果你不想终止占用端口的进程,也可以修改你的Node.js应用,使其使用不同的调试端口。例如,你可以将调试端口改为5859。

    node --debug=5859 app.js
    
  3. 确保node-inspector使用正确的端口: 同样地,你可以通过指定端口启动node-inspector,例如:

    node-inspector --web-port=8080 --debug-port=5859
    

示例代码

下面是你的静态文件服务器的代码示例:

(function (exports, require, module, __filename, __dirname) {
    var http = require('http');
    var url = require('url');
    var fs = require('fs');

    http.createServer(function (req, res) {
        // 截取请求的URL, 端口后后面的部分
        var pathname = url.parse(req.url).pathname;
        var realpath = "public" + pathname;

        fs.exists(realpath, function (exists) {
            console.log(exists);
            if (!exists) {
                res.writeHead(404, {'Content-Type': 'text/plain'});
                res.write("404 not found!");
                res.end();
            } else {
                fs.readFile(realpath, 'utf-8', function (err, data) {
                    if (!err) {
                        console.log(data);
                        res.writeHead(200, {'Content-Type': 'text/html'});
                        res.write(data, 'utf-8');
                        res.end();
                    } else {
                        res.writeHead(500, {'Content-Type': 'text/plain'});
                        res.end(err);
                    }
                });
            }
        });
    }).listen(8888);

    console.log('Server running at http://127.0.0.1:8888/');
});

启动步骤

  1. 启动Node.js应用:

    node --debug=5859 app.js
    
  2. 启动node-inspector

    node-inspector --web-port=8080 --debug-port=5859
    
  3. 在浏览器中访问:

    http://127.0.0.1:8080/debug?port=5859
    

这样就可以避免端口冲突的问题,并正常进行调试。


通过cmd查询当前电脑端口号情况 TCP 127.0.0.1:5858 0.0.0.0:0 LISTENING 5204

5858就被这一个占用,看上去没错~囧

for help …

等了很久也没有答案…不知道是高手都在上班不看论坛,还是现在人气太少~

那我自问自答一下吧…查了很多网上的答案,自己也看了node-inspector的API尝试了端口占用什么的,现在能正常调试,但是问题根源还不好说.

结果很简单,因为我项目是放在中文路径下,改成英文的就好了,nodejs本身好像不纠结这个问题,感觉可能是node-inspector的支持不够吧.

和路径没有关系把,我建议用node --debug-brk ***.js,这个应该没有问题的

根据你的描述,问题出在 node-inspector 和你的 Node.js 应用之间可能存在端口冲突。默认情况下,node-inspector 使用端口 8080 来监听调试请求,而你的应用可能也在使用相同的端口。

你可以通过以下步骤来解决问题:

解决方案

  1. 修改 node-inspector 的端口号

    你可以通过命令行指定 node-inspector 的端口号。例如,使用 9090 端口:

    node-debug --web-port=9090 app.js
    

    或者在命令行中启动 node-inspector 并指定端口:

    node-inspector --web-port=9090
    

    然后在另一个终端窗口中启动你的 Node.js 应用并启用调试模式:

    node --debug=5859 app.js
    
  2. 检查你的应用是否使用了相同的端口

    确保你的应用没有尝试在同一端口上进行调试。你可以在启动应用时指定不同的调试端口:

    node --debug=5860 app.js
    
  3. 代码调整

    如果你在代码中没有显式地设置调试端口,确保没有其他进程占用该端口。可以使用以下命令查找并终止占用端口的进程:

    lsof -i :5858
    kill -9 <PID>
    

示例代码

// app.js
var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function(req, res) {
    var pathname = url.parse(req.url).pathname;
    var realpath = "public" + pathname;

    fs.exists(realpath, function(exists) {
        console.log(exists);
        if (!exists) {
            res.writeHead(404, {'Content-Type': 'text/plain'});
            res.write("404 not found!");
            res.end();
        } else {
            fs.readFile(realpath, 'utf-8', function(err, data) {
                if (!err) {
                    console.log(data);
                    res.writeHead(200, {'Content-Type': 'text/html'});
                    res.write(data, 'utf-8');
                    res.end();
                } else {
                    res.writeHead(500, {'Content-Type': 'text/plain'});
                    res.end(err);
                }
            });
        }
    });
}).listen(8888);

console.log('Server running at http://127.0.0.1:8888/');

启动应用和调试器:

# 启动应用
node --debug=5860 app.js

# 启动 node-inspector
node-inspector --web-port=9090

通过这些步骤,你应该能够解决端口冲突问题,并正常调试你的应用。

回到顶部