To do your best, you must first use the node. js debugging technology.

Source: Internet
Author: User

Debugging and development technologies constitute the foundation of software development. Currently, nodejs, as a new Web server development stack, has attracted the attention of developers. In general, nodejs applications mainly have two parts: JS modules written in JavaScript and binary modules compiled in C. Here we mainly introduce three debugging methods for the avascript module: the built-in debugger Based on nodejs, the V8 debugging plug-in and the Chrome browser. All of the following operations are based on the following code (example. JS ):

var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
var path = url.parse(req.url).pathname;
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(path);
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

 

1. built-in debugger Based on nodejs

Nodejs provides a built-in debugger to help developers debug applications. To enable the debugger, we need to add the debugger tag to the code. When nodejs executes the debugger tag, it will be automatically suspended (the debugger tag is equivalent to opening a breakpoint in the Code ). The Code is as follows:

var path = url.parse(req.url).pathname;
debugger;
res.writeHead(200, {'Content-Type': 'text/plain'});

Run the command: node debug example. js to enter the debugging mode.

In debug mode, you can use built-in commands such as repl to evaluate the values of variables and expressions (as shown in Figure 2 ). You can also use the help command to obtain the complete list of Debugging commands.

Commands: Run (R), cont (C), next (N), step (s), Out (O), backtrace (BT), setbreakpoint (SB ), clearbreakpoint (CB ),
Watch, unwatch, watchers, REPL, restart, kill, list, scripts, breakpoints, version

 

2. V8 Plugin-based Debugger
Nodejs is built on the Google V8 engine. Google provides eclipse with a corresponding debugging plug-in. On how to install and debug nodejs programs in eclipse will not be repeated descriptions, there are already a lot of articles on the Internet (Specific can refer to this article http://cnodejs.org/blog? P = 911 ). Note that the debug mode supported by V8 is local by default. To enable remote debugging, we need to modify the V8 source file in nodejs:/deps/V8/src/platform-posix.cc

  addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); --> INADDR_ANY
addr.sin_port = htons(port);

Then re-compile nodejs.

Tip:

When you use plug-ins to debug nodejs programs, you may encounter connection refuse, get version failed, and other errors. Pay attention to the IP address you are using. Generally, the loopback address of 127.0.0.1 works. If you use a real IP address, check the firewall settings.

 

3. Chrome browser-based Debugger

Since we can debug the plug-in through V8, can we also use the Javascript debugger of chrome to debug it? The node-Inspector module provides such a possibility. We need to install node-Inspector through NPM first

NPM install-G node-Inspector // import the installation path to the Environment Variable

Node-Inspector switches to debug input and output through websocket. Therefore, we need to start node-inspector to listen to the debug debugging port of nodejs before debugging.

By default, the node-Inspector port is 8080. You can set the port through the -- web-Port = [port] parameter. After starting node-inpspector, we can start nodejs through -- debug or -- debug-BRK. Enter http: // [IP address]: 8080/debug? in the browser? Port = 5858, we will get the following debugging window:

These three methods have their respective advantages and disadvantages. I personally prefer the node-Inspector method.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.