With a few days of node. js feel very novel, but debugging problem is really worry people, the beginning of lazy learning debugging method, look at the abnormal content can be, but with the code complexity of the rise, not all errors are grammatical errors, do not debug, have to search information, learn how to debug.
No supervisor to restart the service every time
The students who have used PHP must be aware that when a script file is modified, the new content is loaded as soon as the page server is refreshed. But node. js in the first reference to a file after parsing will put it into memory, the next time access to directly in memory to improve efficiency, but this is a problem for our development, modified a module can only restart after the server to take effect, debugging efficiency is still very low.
As a result, node. JS has the Supervisor plugin to help us make solid file changes, automatically restart the server, Supervisor is a node. js package, installation is simple, using NPM installation commands can be, because we need to run in the console, so we need to install in the global environment
NPM install-g Supervisor
So we can use Supervisor to start the script.
Supervisor Index
When we make changes to the file, we can see that the console has more than three lines, the server has been restarted
Native Console debugging
node. js natively supports debugging, and adding a breakpoint before the statement is preceded by a debugger instruction
var server=require ('./server '), router=require ('./router '), requesthandlers=require ('./requesthandlers ') );d Ebugger;var handle={};d ebugger;handle['/']=handle['/start ']=requesthandlers.start;debugger;handle['/upload '] =requesthandlers.upload;handle['/show ']=requesthandlers.show;debugger;server.start (8080,router.route,handle);
Adding debug options When starting a service
Node Debug index.js
At this time to enter some instructions can be single-step debugging, to the breakpoint monitoring local variables, such as a command diagram, a lot of commands have their abbreviated form
node. js Debug Command
Command |
Function |
Run |
Execute script, pause on first line |
Restart |
Re-execute the script |
Cont, C |
Continue execution until the next breakpoint is encountered |
Next, N |
Single Step execution |
Step, S |
Step into and enter the function |
Out, O |
Stepping out of a function |
Setbreakpoint (), SB () |
The current line sets a breakpoint |
Setbreakpoint (' F () '), SB (...) |
Set a breakpoint on the first line of function f |
Setbreakpoint (' Script.js '), SB (...) |
Set breakpoints on line 20th of Script.js |
Clearbreakpoint, CB (...) |
Clear All Breakpoints |
BackTrace, BT |
Displays the current call stack |
List (5) |
Shows the front and back 5 lines of code that are currently executing |
Watch (expr) |
Adding expression expr to the watch list |
Unwatch (expr) |
Removing expression expr from the watch list |
Watchers |
Display all expressions and values in the watch list |
Repl |
Open an immediate evaluation environment in the current context |
Kill |
Terminates the currently executing script |
Scripts |
Show all scripts that are currently loaded |
Version |
Show V8 version |
Detailed use of interested students can explore their own, I am not interested in ... It's too complicated to see a few intimate
Using Eclipse debugging
Yes, Eclipse is mighty, and even node. js can debug, download Eclipse on the Eclipe website, and Help->install New Software->add
Add a source in the popup window, the name is good to remember, the address is http://chromedevtools.googlecode.com/svn/update/dev/.
After a while, pop up the selection screen and select the first
All the way next to the final finish, the download will remind you to restart eclipse after completion, you can debug node. js, open the file you want to debug, switch Eclipse to debug view, click the small triangle on the right side of the toolbar, select Debug Configuration
Double-click the standard V8 VM option to create a new configuration and fill in the appropriate parameters.
To start the node server in the console via the--DEBUG-BRK option
Node--debug-brk=5858 test.js
Click the Debug button on the Eclipse interface just to debug node. js as you would debug Java
Debugging with Node-inspector
Most of the node. JS applications are Web applications, so some chrome-based online debugging tools came into being, the most famous should be Node-inspector, this is a node. js module, installation, use is quite convenient, First use NPM to install it in the global environment
NPM install-g Node-inspector
The node-inspector is switched to the debug input and output by WebSocket mode. Therefore, we need to start node-inspector to listen to the debug debug port of node. js before debugging. By default, the port of Node-inspector is 8080 and can be set by parameter--web-port=[port].
After starting Node-inpspector, we can start the node. JS program by--debug or--DEBUG-BRK.
At this time you can access http://127.0.0.1:8888/debug?port=5858 using browser debugging, look at the interface, no need to say anything more
At last
Reference: node. JS Development Guide
PS: Personally think the last one is the most convenient
node. JS Debugging