In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to debug nodejs programs, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
For developers, in the process of developing applications, they often need to rely on the debugging functions of programming languages in order to develop conveniently and solve the problem of bug. Generally speaking, we need to do this with the help of the powerful debugging capabilities of IDE. Nodejs is no exception.
Today we will describe in detail how to debug node programs.
Enable debugging of nodejs
Remember the koa program we talked about earlier? This article will take a simple koa server program as an example to expand the debugging of nodejs.
Let's take a look at a simple koa service app.js:
Const Koa = require ('koa'); const app = module.exports = new Koa (); app.use (async function (ctx) {ctx.body =' Hello World';}); if (! module.parent) app.listen (3000)
The above program opens port 3000 and sets up a http service. Hello World is returned for each request, which is very simple.
To run the above program, we need to execute node app.js. This executes app.js but does not turn on debugging.
How to debug?
We need to add the-- inspect parameter:
Node-inspect app.js
The above code will turn on the debugging function of nodejs.
Let's take a look at the output:
The result tells us two things. The first thing is the port on which debugger listens. Port 9229 of 127.0.0.1 will be opened by default. And a unique UUID is assigned to distinguish.
The second thing is to tell us that the debugger used by nodejs is Inspector.
Inspector was introduced after nodejs 8, and if it was before nodejs 7, legacy debugger was used.
Security of debugging
If debugger is connected to the nodejs runtime environment, if there is a malicious attacker, the malicious attacker can run arbitrary code in the nodejs environment. This will bring great security risks to our program.
So we must pay attention to the safety of debugging. In general, remote debugging is not recommended.
By default-- inspect is bound to 127.0.0.1, which allows only local programs to access it. And any locally running program has permission to debug the program.
If we really want to expose the debug program to external programs, we can specify the local extranet IP address or 0.0.0.0 (for any address, unlimited), so that the remote machine can debug remotely.
What should we do if we want to have a secure remote debug?
Node-inspect app.js
Then we can build a ssh tunnel to map the local port 9221 to the remote server port 9229:
Ssh-L 9221:localhost:9229 user@remote.example.com
In this way, we can debug remotely by connecting to local port 9221.
Using WebStorm for nodejs debugging
WebStorm produced by JetBrains is a powerful tool for developing nodejs. WebStorm has its own debug option. If this option is enabled, it will be enabled in the background-- inspect:
Using WebStorm for debugging is similar to using IDEA for java program debugging, which is not covered here.
Debugging with Chrome devTools
The premise of using Chrome devTools for debugging is that we have turned on-- inspect mode.
We can see the interface of chrome inspect, and if you already have a nodejs program that opens inspect locally, you can see it directly in Remote Target.
Select the target you want to debug and click inspect to open the Chrome devTools debugging tool:
You can profile the program or debug it.
Here we focus on debugging, so go to the source column and add the source code of the program you want to debug:
Add a breakpoint and you can start debugging. It's the same as debugging js on the web side in chrome.
Use node-inspect for debugging
In fact, nodejs has a built-in debugging tool called node-inspect, which is a debugging tool for cli. Let's see how to use it.
We directly use:
Node inspect app.js
< Debugger listening on ws://127.0.0.1:9229/f1c64736-47a1-42c9-9e9e-f2665073d3eb< For help, see: https://nodejs.org/en/docs/inspector< Debugger attached.Break on start in app.js:1>1 const Koa = require ('koa'); 2 const app = module.exports = new Koa (); 3 debug >
Node inspect does two things, the first thing is to generate a subroutine to run node-- inspect app.js, and the second thing is to run the CLI debug window in the main program.
This CLI debugger provides us with some very useful commands:
1 、 Stepping
Cont, c: continue execution
Step, s: Step in
Out, o: Step out
Pause: code that is suspended
2 、 Breakpoints
SetBreakpoint (), sb (): sets a breakpoint on the current line
SetBreakpoint (line), sb (line): sets a breakpoint on the specified line
SetBreakpoint ('fn ()'), sb (…) : sets a breakpoint in the specified function
SetBreakpoint ('script.js', 1), sb (…) Set breakpoints in the specified script file
ClearBreakpoint ('script.js', 1), cb (…)
3 、 Information
Backtrace, bt: print the backtrace information of the current execution frame
List (5): list 5 lines before and after the source code
Watch (expr): add a listening expression
Unwatch (expr): delete listening expression
Watchers: list all watchers
Repl: open repl expression
Exec expr: execute expression
With the above command, we can do more complex debugging activities in CLI.
Other debug clients
In addition to the several we mentioned above, we can also use vscode,Visual Studio, Eclipse IDE, etc., to debug nodejs, which will not be described in detail here.
The above is how to debug nodejs programs. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.