【问题标题】:node inspector can not work without specify port节点检查器不能在没有指定端口的情况下工作
【发布时间】:2023-03-07 22:40:01
【问题描述】:

我是节点新手。我尝试创建一个仅接收 POST 请求并将传入的 POST 正文字符转换为大写并将其返回给客户端的 HTTP 服务器。我的服务器监听程序第一个参数提供的端口。

我的代码:

以下是我的代码:

var map = require('through2-map');
var http = require('http');

http.createServer(function (req, res) {
    debugger;
  console.log("typeof req");
  if (req.method === "POST") {
    req.pipe(map(function (chunk) {
      return chunk.toString().toUpperCase();
    })).pipe(res);
  }
}).listen(process.argv[2]);

此程序需要来自命令行的端口参数。

无调试:

我运行服务器:$ node http_uppercaserer.js 8080

并测试它:$ curl 127.0.0.1:8080 -X POST -d "asd",它返回给我ASD

这意味着工作正常。

用检查器调试:

但是当我使用node inspector 调试我的程序时:$ node-debug http_uppercaserer.js 8080, 并测试它:$ curl 127.0.0.1:8080 -X POST -d "asd",

它返回给我:Cannot POST /

奇怪

运行node-debug http_uppercaserer.js 8080后,默认在第一行中断,当我点击恢复时,终端会报错:

$ node-debug http_uppercaserer.js 8080
Node Inspector is now available from http://localhost:8080/debug?port=5858
Debugging `http_uppercaserer.js`

debugger listening on port 5858

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> (/Users/tangmonk/Documents/learn/nodejs_study/async_and_http/http_uppercaserer.js:12:4)
    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 Module.runMain [as _onTimeout] (module.js:497:10)

但是如果我指定端口$ node-debug -p 9000 http_uppercaserer.js 8080,它可以正常工作,为什么?

【问题讨论】:

    标签: node.js node-inspector node-debugger


    【解决方案1】:

    node-debug 默认添加--debug-brk 选项,这将中断第一行的代码执行。你将不得不恢复它。所以做完之后:

    node-debug -p 8082 e.js 8081
    

    在运行 curl 之前在 chrome 中打开链接并恢复执行。或者你可以这样做:

    node-inspector  --web-port=8082
    node --debug=5858 /datastorage/rupayan/e.js 8081
    

    那么你就可以正常使用了

    curl 127.0.0.1:8081 -X POST -d "asd"
    

    PS:抱歉,我无法在默认 8080 上运行我的节点检查器。所以如果你可以使用 8080 端口,则无需提供 8082。

    奇怪的部分答案

    这是因为 node-inspector 本身托管了一个服务器,并且它的默认地址是 8080。所以你的应用程序和 node-inspector 都不能使用它。

    【讨论】:

    • 感谢您的回复,您误会了,我的程序需要一个端口参数形式的命令行,而不是更改node_debug's 端口
    • 谢谢,您的解决方案有效!但我很奇怪,我更新了我的问题(weird 部分),你能再帮我一次吗?
    猜你喜欢
    • 2020-04-23
    • 2018-01-10
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2018-11-04
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多