没有指定端口,节点检查器无法工作

时间:2014-06-17 04:38:28

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

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

我的代码:

以下是我的代码:

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,它可以正常工作,为什么?

1 个答案:

答案 0 :(得分:3)

默认情况下,

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.因此,您的应用程序和节点检查员都无法使用它。

相关问题