It is important to navigate between the code you want to inspect. It would be tedious and unnecessary to go through every line of code. The debugger provides a convenient way to see what's important and jump out of unimportant blocks of code. Let's see how to enter, skip, and exit functions while debugging!
In Previous article, we studied the VS Code debugger, added breakpoints in the code, and also viewed the local status.
This time, we'll learn how to step through code line by line and how to jump in and out of function calls.
Get the code
First, let’s make the last server modification more complex. Add two extra functions: one to get the name from the request and another to generate the greeting.
You can paste the following code into index.js
.
const http = require('http'); const url = require('url'); const hostname = '127.0.0.1'; const port = 3456; const serverUrl = `http://${hostname}:${port}` const getNameFromReq = (req) => { const {name} = url.parse(req.url, true).query; return name } const getGreeting = (name) => { const greeting = `Hello, ${name}!` return greeting } const server = http.createServer((req, res) => { const name = getNameFromReq(req) const greeting = getGreeting(name) res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end(`${greeting}\n`); }); server.listen(port, hostname, () => { console.log(`Server running at ${serverUrl}`); });
The code of this series can be obtained at https://github.com/thekarel/debug-anything
Start the debugger
Let's start the debugger: Use the debug toolbar or press F5
and select Node.js
:
You should be able to access http://127.0.0.1:3456/?name=Coco
and See the greeting.
If you like the command line, you can also use curl http://127.0.0.1:3456\?name\=Coco
to access.
Okay, now that the server is up and running, let's add a breakpoint. The debugger will not start without a breakpoint:
Add a breakpoint on line 21:
const name = getNameFromReq(req)
Step by Step debugging
Trigger the pair againhttp://127.0.0.1: 3456/?name=Coco
, the debugger will be activated and stop at line 21 of the code:
##pretty! Now let's focus on the
Debug Toolbar:
The first thing is the name of the button. The order from left to right is as follows (with default VS Code shortcuts): Continue, restart and stop are very simple and will perform the operations you expect respectively: Continue to the next breakpoint, restart the process, and stop the process (and debugger). Step is related to the function call on the current line: you can step into a function call (Step Over), enter the function call (Step Into internally view and debug), or leave the function. (Step Out). Step-over operations also allow you to execute code line by line, even if the line is not a function call. The Step command only controls what you see in the debugger. So "Step Out" or "Over" a function willstill execute all code as usual. The debugger won't make you bored, and you'll be able to complete your main work faster.
Continue
Continue will run the code until the next breakpoint or the end of the program. One way to debug is to add multiple breakpoints on the relevant lines beforehand and jump between them with continue:If you Already know which functions or lines are relevant for your purpose, then the Continue operation will be very convenient. The debugger will pause at a predefined location so you can inspect variables and the call stack.
Step Over
You can think of Step Over as stepping through the function line by line, but without entering the function call. Use it if you are not interested in the internal logic of the function call in the current line, but just want to see how local variables change over time, for example:Step Over is a good way to skip
declarative code.
Step Into
When a line calls a function that interests you and you want to dig deeper, you can use Step Into. Once inside the code block, you can debug as usual (using continue, step, etc.).
Observe how we skip getNameFromReq
and then enter getGreeting
:
Step Out
Step Out is the opposite of Step In: if you are no longer interested in a function, you can leave it. Using "Step out" will run the rest of the function's code in one go.
Check the difference between these two functions through debugging. We execute the first function line by line, but exit the second function early:
Now, you should have a better understanding of the debugger toolbar, how to focus on the important things and skip the irrelevant parts. Not only will these commands save you time, they will make the entire debugging job more enjoyable! Why not try it in your project?
VSCode debugging tutorial series:
English original address: https://charlesagile.com/debug-javascript-typescript-debugger-navigating-with-steps
Author: Charles Szilagyi
Recommended related tutorials: vscode introductory tutorial
The above is the detailed content of VSCode debugging tutorial (2): step-by-step debugging. For more information, please follow other related articles on the PHP Chinese website!