Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I have built a simple server, and it work perfect when start server with command node server.js. I can use Postman, or some other tools to invoke a GET/POST request.

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.

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.

How can make a request to debugging server?

like image 939
t4nhpt Avatar asked Jul 04 '15 11:07

t4nhpt


People also ask

How do I use the node debugger in Visual Studio Code?

Use a launch config to start your program, or attach to a process launched outside of VS Code. If the Auto Attach feature is enabled, the Node debugger automatically attaches to certain Node.js processes that have been launched from VS Code's Integrated Terminal.

What is Node JS debugging?

In JavaScript, the first stop for a debugging task is often logging to the console, but using a debugger can give us a more integrated experience. Node js is a cross platform and open source JavaScript runtime environment that allows the JavaScript to be run on the server-side.

How do I stop a debug session in Node JS?

Using the Debug: Stop action (available in the Debug toolbar or via the Command Palette) stops the debug session. If the debug session was started in "attach" mode (and the red terminate button in the Debug toolbar shows a superimposed "plug"), pressing Stop disconnects the Node.js debugger from the debuggee that then continues execution.

Can expressjs be used for REST API?

Even though we are only going to build a REST API in this guide, the ExpressJS framework is not limited to just that - hosting static files, performing server-side rendering, or even using it as a proxy server isn't uncommon and the sky's the limit with additional middleware.


2 Answers

Here is my solution: Do not use node-debug *.js directly. Instead, open two terminals in your project. For example, I want to debug server-3.js, like the pictures down here. In one terminal, use node-inspector for debugging listening, and another use node --debug server-3.js

enter image description here

enter image description here

after sending a request on postman: enter image description here

you can see your debugging situation in your node-inspector window listening on port 5858:

enter image description here

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.

enter image description here

like image 169
athanzhang Avatar answered Sep 20 '22 17:09

athanzhang


Try with nodedev

1) Install nodedev:

sudo npm install -g nodedev

2) Start your server:

nodedev server.js

3) Visit the page http://127.0.0.1:7001/debug?port=7000 to debug...

4) ...

5) Profit!

Bonus: Your server will automatically restart when changes are detected!

like image 35
malix Avatar answered Sep 23 '22 17:09

malix