Learning a new technology will definitely come across a lot of pits, and we need to find these pits and figure out the causes and principles of these pits. This operation is called debugging.
Program debugging methods and tools are various, here I summarize my learning nodejs process, learned and used debugging methods.
Log
Console.log directly in JavaScript code, you can print information in the console. But this kind of function is too monotonous, the project module many, the function is complex, if does not have a contract good Console.log method, very easily causes the printing information to be very messy, the readability is very poor.
The Nodejs has a debug module that provides:
Define the Log module and select a specific module log output
Module text color highlighting
Log Time record
Output log to file functions
First of all
NPM init, NPM install debug--save Create a new Nodejs project and install the Debug module
And then create a new
App.js
var debug=require ("Debug") ("Mydebug:http"), work =require ("./work"), http= Require ("http"); Http.createserver (function(req,res) { + "+ Req.url); Res.end (' hello\n ');}). Listen (,function() { debug ("Listening");});
Work.js
var debug=require ("Debug") ("Mydebug:work"),setinterval (function() { debug ( "Doing some work @%s--%s",new Date (). GetTime (), "with supervisor");},2000);
The mydebug:http and mydebug:work two log modules were created in the above two files, and the log module to be printed can be configured when the project is launched, which supports wildcard matching.
booting in Linux: Debug=mydebug:* node app.js start set debug=mydebug:* & node App.js in Windows
This allows you to see the log print for different modules and also to see the log output time.
Accessing localhost:3000 in the browser can also see the printed access information
In addition, the debug module provides the ability to output logs to a file
Set debug=mydebug:* & node app.js mydebug:work> debug.log
Nodejs Debug Module Documentation: HTTPS://GITHUB.COM/VISIONMEDIA/DEBUG
Debug
Log is not enough, when the program has a problem with the log can be located in the wrong location, but when we want to see the wrong site variables, log is powerless, in general, we do not print all the variables. At this point, you need the function of the breakpoint, in the program inside the breakpoint, directly to the wrong location, analysis error site to confirm the cause of the error.
The Nodejs internally provides a debug mechanism that allows the program to enter debug mode, allowing the developer to analyze code discovery issues Step-by-step.
A total of 3 in the start parameter can let the program into debug mode
----DEBUG-BRK app.js
There are some differences between the 3 modes in the debugging form.
Node Debug app.js
1. This way to start the program, the program will enter the debug mode, and run to the start of the file on the 1th line to stop, waiting for the developer to go down the command
2. Start the program in this way and enter debug mode directly in the current CMD
Node--debug app.js
1. Starting the program this way, the program goes into debug mode and runs through all the code. This type of startup is often used during program startup without debugging, by triggering time into the callback function, such as a breakpoint in an HTTP request, waiting for the client to enter the breakpoint after access
2. This way to start the program, will open a TCP port monitoring, in this CMD does not enter the debug mode, you need to open the terminal with the node debug command to connect the debug port
Command for node debug localhost debug port
or node debug p node process ID
Node--debug-brk app.js
1. This way to start the program, the program will enter debug mode, but will not run the code, until a terminal connected to the debug port, to start executing code, and enter a breakpoint on line 1th
2. This way to start the program, will open a TCP port monitoring, in this CMD does not enter the debug mode, you need to open the terminal with the node debug command to connect the debug port
After entering debug mode, you can set breakpoints, cancel breakpoints, and control program execution flow with some commands.
Command document: Https://nodejs.org/api/debugger.html#debugger_commands_reference
Process Control related
cont
, c
-Continue execution
next
, n
-Step Next
step
, s
-Step in
out
, o
-Step out
pause
-Pause running code (like pause button in Developer Tools)
Breakpoint setting Cancellation related
setBreakpoint()
, sb()
-Set breakpoint on
setBreakpoint(line)
, sb(line)
-Set breakpoint on specific line
setBreakpoint(‘fn()‘)
, sb(...)
-Set breakpoint on a first statement in functions body
setBreakpoint(‘script.js‘, 1)
, sb(...)
-Set breakpoint on first line of Script.js
clearBreakpoint(‘script.js‘, 1)
, cb(...)
-Clear breakpoint in script.js on line 1
Variable View related
backtrace
, bt
-Print backtrace of current execution frame
list(5)
-List Scripts source code with 5 line context (5 lines before and after)
watch(expr)
-ADD Expression to watch list
unwatch(expr)
-Remove expression from watch list
watchers
-List all watchers and their values (automatically listed in each breakpoint)
repl
-Open Debugger ' s repl for evaluation in debugging script ' s context
Variable names can be entered in REPL mode to view variable contents
Node Debug
Enter the breakpoint from the first line of code and command N to enter the next line
Node--debug
CMD1 opening the Debug port
CMD2 Connecting the Debug port
Set breakpoints, cancel breakpoints
Cmd1 a minute to print.
Connect the Debug module in the same way as the process ID
You can see that the PID is 4436
REPL mode
Debugging related tools and modules
The above debugging process is still a little cumbersome, and there are tools and node modules that can be used to assist debugging.
Supervisor
Supervisor is a node module that is used to start the node project.
Supervisor can monitor some files and automatically refresh the program when these files change, without restarting the node program.
When the program crashes, supervisor restarts the program. There are other configurations that you can see in the Supervisor documentation for https://github.com/petruisfan/node-supervisor/installation supervisor
NPM install-g Supervisor
Monitor work.js changes and start the node program
Modify the debug information in work
End the App.js node process in Task Manager and you can see that supervisor automatically restarts the app.js process
Webstorm
Webstorm provides a more convenient debug tool
Run-debug-app.js in the menu
You can click directly on the line number and hit the breakpoint.
Browser Access localhost:3000, enter breakpoint
You can see some debugging tools provided by Webstorm
In fact, the Webstorm debugging function is also based on the--DEBUG-BRK, using the 63797 port to debug
Node-inspector
If you don't like Webstorm's debugging tools, you can also use our familiar Chrome debugging tools to debug your node code, but you'll need to install a node module--node-inspector
NPM install-g Node-inspector
When the installation is complete, open a node debug port 12345
Then open a new cmd, start a node-inspector debugging service, connect to the debug port you just opened
Follow the prompts to access the address to debug Nodejs code using our familiar Chrome debugging tools
Debugging skills have a lot, a lot of detail problems need different debugging skills to achieve, later to use the new to add it ~
Start to learn nodejs--debugging Chapter