简体   繁体   English

使用node-inspector调试节点/ Express RESTful API

[英]Debugging Node/Express RESTful API with node-inspector

I have seen many question about debugging a Node/Express application, but, it seem a node web application, not a RESTful server. 我已经看到很多关于调试Node / Express应用程序的问题,但是,它似乎是节点Web应用程序,而不是RESTful服务器。

I have built a simple server, and it work perfect when start server with command node server.js . 我已经构建了一个简单的服务器,它在使用命令node server.js启动服务器时非常有效。 I can use Postman, or some other tools to invoke a GET/POST request. 我可以使用Postman或其他一些工具来调用GET / POST请求。

After installing the node-inspector module, when I start a node-inspector debug with command node-debug server.js , it show me a webpage at http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 with inspector window. 安装node-inspector模块后,当我使用命令node-debug server.js启动node-inspector调试时,它会显示一个网页http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858带检查员窗口。

Then, I use postman to invoke request again (which has invoked successfully earlier), but it show me a 404 error : Cannot GET /api/v1/user/login . 然后,我使用邮递员再次调用请求(之前已成功调用),但它显示404 errorCannot GET /api/v1/user/login

How can make a request to debugging server? 如何向调试服务器发出请求?

Try with nodedev 尝试使用nodedev

1) Install nodedev : 1)安装nodedev

sudo npm install -g nodedev

2) Start your server: 2)启动服务器:

nodedev server.js

3) Visit the page http://127.0.0.1:7001/debug?port=7000 to debug... 3)访问页面http://127.0.0.1:7001/debug?port=7000进行调试...

4) ... 4)......

5) Profit! 5)利润!

Bonus : Your server will automatically restart when changes are detected! 额外奖励 :检测到更改后,您的服务器将自动重启!

Here is my solution: Do not use node-debug *.js directly. 这是我的解决方案:不要直接使用node-debug * .js。 Instead, open two terminals in your project. 而是在项目中打开两个终端。 For example, I want to debug server-3.js, like the pictures down here. 例如,我想调试server-3.js,就像这里的图片一样。 In one terminal, use node-inspector for debugging listening, and another use node --debug server-3.js 在一个终端中,使用node-inspector进行侦听侦听,另一个使用节点--debug server-3.js

在此输入图像描述

在此输入图像描述

after sending a request on postman: 在邮递员发送请求后: 在此输入图像描述

you can see your debugging situation in your node-inspector window listening on port 5858: 您可以在端口5858上的节点检查器窗口中查看调试情况:

在此输入图像描述

I believe this is what you want to achieve. 我相信这就是你想要实现的目标。

The latest update is that you can also resort to vscode, which has a plugin called "Debugger for Chrome" that has the debug function you want. 最新的更新是你也可以使用vscode,它有一个名为“Debugger for Chrome”的插件,它具有你想要的调试功能。

在此输入图像描述

使用node-inspector而不是node-debug(来自同一模块)在一个终端中执行: node-inspector在另一个终端中执行node --debug server.js

Note that --debug option is deprecated. 请注意,不推荐使用--debug选项。 --inspect is preferred. --inspect是首选。 Otherwise you will get a warning/error. 否则你会收到警告/错误。

(node:11524) [DEP0062] DeprecationWarning: node --debug and node --debug-brk are invalid. (节点:11524)[DEP0062] DeprecationWarning: node --debugnode --debug-brk无效。 Please use node --inspect or node --inspect-brk instead. 请使用node --inspectnode --inspect-brk代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM