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?
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.
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.
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.
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.
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
after sending a request on postman:
you can see your debugging situation in your node-inspector window listening on port 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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With