Talking about Node debugging tools to get started

JavaScript procedures are increasingly complex, and the importance of debugging tools is increasing. How does the client script have a browser, how does the node script debug?

浅谈Node 调试工具入门教程 2016, Node decided to use the “Developer Tool” of the Chrome as an official debugging tool so that the Node script can also use the graphical interface to debug, this is great convenient. Developer.

This article describes how to use the Node script debugging tool.

First, the exemplary procedure

For convenience of explanation, the following is a sample script. First, create a new work directory and enter the directory.

$ mkdir debug-demo $ cd debug-demo
, then generate  package.json  File and install the KOA framework and KOA-ROUTE module. 
$ npm init -y $ npm install –save koa koa-route

Next, create a new script
App.js and writes the following.
  // App.jsconst KOA = Require ('Koa'); const router = Require ('Koa-route'); const app = new koa (); const Main = ctx => {ctx.response.body = 'Hello World';}; const welcome = (ctx, name) => {CTX.Response.body= 'Hello' + Name;}; app.use (router.get ('/', main)); app.use ('/: name', welcome); app.listen (3000); Console.log ('Listening On Port 3000');  
The above code is a simple web application that specifies two routing. When access, a welcome information will be displayed. If you want to understand the detailed meaning of the code in detail, you can refer to the KOA tutorial.
Second, launching developer tools

Now run the above script.

$ node –inspect app.js
  Among the above code,  - Inspect 
The parameter is required to start the debug mode. At this time, open the browser to access http://127.0.0.1//3000, you can see Hello World.

Next, it is necessary to start debugging. There are two ways to open the debug tool, the first is to type in the address bar of the Chrome browser, type

chrome: // inspect

or about: inspect , after entering! See the interface below.

In the Target section, click the Inspect link to enter the debug tool.
 The second method of entering the debugging tool is to open the "Developer Tool" at http://127.0.0.0.1//3000 window. There is a green flag on the top left corner. Click to enter .   
Third, the debugging tool window

debugging tool is actually “Developer Tools” customized version, eliminating the need for those parts of the server script useless.

It has four main panels. 浅谈Node 调试工具入门教程

Console: Console Memory: Memory Profiler: Performance

Sources: Source 浅谈Node 调试工具入门教程

the use of these panels, with the browser environment basically the same, here only Sources (source) panel.

浅谈Node 调试工具入门教程

Fourth, set a breakpoint

into the Sources panel, find a running script

app.js

. Tap the

  1. In line 11 (i.e. below the line) the line number, to set a breakpoint.
  2. ctx.response.body = ‘Hello’ + name;

浅谈Node 调试工具入门教程

At this point, browser access http://127.0.0.1:3000/alice, the page will show is waiting for the server to return. Switch to the debugging tool, you can see Node main thread is suspended (paused) stage.

into the Console panel, input, name, will return alice. This shows that we are in the context of the breakpoint (context).

浅谈Node 调试工具入门教程 Sources then cut back panel, the right side can be seen Watch, Call Stack, Scope, Breakpoints other drawer. Scope drawer open, see Local Scope and GAll variables in the Lobal action domain.

Local action domain, the value of the variable

Name

is
Alice , double-click to enter the editing state, change it to
 Bob .  
Then click on the continued run button on the top toolbar.

浅谈Node 调试工具入门教程

The Hello Bob can be seen on the page.

The command line, press CTRL + C to terminate the operation 浅谈Node 调试工具入门教程 app.js

.

Five, debugging non-service scripts 浅谈Node 调试工具入门教程

Web service script will have been run in the background, but most of the scripts are just to deal with a task, and they will terminate. At this time, you may have no time to open the debug tool at all. When you open, the script has long run. How do you debug it at this time?

$ node --inspect = 9229 -e "settimeout (Function () {console.log ('yes');}, 30000) In the above code, – Inspect = 9229

specifies that the debug port is 9229, which is the default communication port of the debug tool.

-e 浅谈Node 调试工具入门教程 Parameter Specifies a string as a code run.

Access

Chrome: // Inspect

, you can enter the debugging tool, debug this code.

浅谈Node 调试工具入门教程

The code is placed in

setTimeout

, it is always less convenient. Scripts of less running time may not be able to open debugging tools at all. The following method is used at this time.

浅谈Node 调试工具入门教程

$ node –inspect-brk = 9229 app.js

In the code above, – inspect-brk

specified in the second set a breakpoint on the line. In other words, a running, is paused.

Six, forget to write –inspect how to do?
  to open the debugging tools on the premise that it is coupled with  Node startup scripts - inspect 
parameters. If you forget this parameter you can not debug it?
The answer is yes. First, the normal startup script.

$ node app.js

Then, in another command line window, find the process ID of the above script .

浅谈Node 调试工具入门教程

$ ps ax | grep app.js 30464 pts / 11 Sl + 0:00 node app.js30541 pts / 12 S + 0:00 grep app.js

 in the above command,  app.js  the process ID is 
30464 .

Next, run the following command.

$ node -e ‘process._debugProcess (30464)’

The above command will create and debug process 30464 tool connection, then you can turn on debugging tool.

Another way is to send SIGUSR1 signal to the script process, debug connection can be established.

   
The above is all the contents of this article, I hope that everyone’s learning is helpful, I hope everyone supports Tumi Cloud.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment