This course will be retired on February 24, 2020. We recommend "Build a REST API With Express" for up-to-date content.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We’re going to debug our app with node-inspector. Node-inspector is a tool that let’s you set breakpoints and step through your code.
Resources
Follow Along
Here's how to get the code to follow along with this video:
git clone https://github.com/treehouse-projects/mean-todo.git
cd mean-todo
git checkout tags/s4v3 -b debugging-express-application-refresher
npm install
-
0:00
An essential tool for developing node applications is Iron Node.
-
0:04
Iron Node let's us interactively debug applications in the browser
-
0:08
while the node application is running.
-
0:11
In this video, we'll install Iron Node and get set up with some basic usage.
-
0:17
To install Iron Node, we'll use npm with a -g flag.
-
0:24
This is because we want to install iron-node globally and
-
0:27
not in our node modules folder.
-
0:30
By installing it globally, we can use it for any node application on our machine.
-
0:42
When it comes to actually debugging applications,
-
0:45
break points are the core aspect of how you'll do it.
-
0:49
In Iron Node, there are two ways to set break points.
-
0:53
The first way is by writing a permanent break point into your application code.
-
1:00
And that looks like this.
-
1:02
Just writing in the debugger statement.
-
1:05
Now the way we'll use iron-node is just like we would use the node command, so
-
1:10
here I'll write iron-node instead of node, and then I'll provide a path to my server.
-
1:16
Iron-node launches our app and we can start setting break points.
-
1:21
Now when I refresh the page Iron Node stops at my debugger statement.
-
1:28
Now I'm also going to set a debugger statement in our API index.js file.
-
1:35
And notice.
-
1:41
I can actually edit this code right inside of Iron Node.
-
1:46
Now it's up to you how much editing you want to do directly in Iron Node.
-
1:51
I know for me I tend to use a text editor to do all of my heavy duty editing
-
1:56
because there's extra tools but
-
1:58
it's perfectly fine to make minor edits to your code directly in Iron Node.
-
2:04
Now I save the file.
-
2:07
I hit my first debugger statement.
-
2:10
I press play.
-
2:11
Now my server's actually running.
-
2:15
Now when I visit my landing page, the get request is run.
-
2:19
Notice my to dos haven't shown up.
-
2:21
And that's because the debugger stop in the middle of my get request.
-
2:26
Notice one other thing they're actually two versions of your files loaded
-
2:31
into Iron Node.
-
2:34
This version of the file is loaded into memory,
-
2:37
actually allows you interactively set break points.
-
2:42
And these are called temporary break points on the lines.
-
2:46
I can set these break points on any line.
-
2:53
And I can also turn them off and turn them on, or
-
2:56
turn all of them off, or turn all of them on at once.
-
3:02
Notice that the file was automatically loaded, when a debugger statement was hit.
-
3:09
The other way to manually load one of these files into memory, inside of
-
3:13
Iron Node, is with the Cmd+P function, and then you can search for your file.
-
3:20
So in our case let's pretend app.js wasn't loaded yet.
-
3:23
Type in app.js, okay there's a lot of files,
-
3:31
Type in, sourceapp.js, okay there's still a lot of files but notice that
-
3:36
the top two files match what I'm typing the second file here is your source file.
-
3:43
The first file here,
-
3:44
where it says no domain means that its been loaded into memory.
-
3:50
And so you can interactively set break points in this file.
-
3:55
So again, notice that this is wrapped with a few
-
4:00
different parameters friendly for Iron Node.
-
4:08
Then my actual source file looks just like we've written it.
-
4:13
And if you want to easily get back to your break point, you can go to the call stack.
-
4:20
You can actually click on anything that is being run.
-
4:23
This is our express back end here.
-
4:26
We are only going to debug our source code.
-
4:29
The magic of debugging is that any time
-
4:33
I'm stopped a break point I can actually hover over a variable.
-
4:37
To see what that variable is assigned to.
-
4:41
Here I see my to do objects.
-
4:47
I can also look at the local variables, the variables inside
-
4:52
of this given closure, as well as my global variables.
-
4:58
There's always going to be quite a few global variables.
-
5:03
And the final way to work with variables locally is actually in your console,
-
5:11
I can type in the to dos variable in our console.
-
5:17
I can actually type in the response or the request object as well to explore it.
-
5:25
And I can even run functions if I wanted to.
-
5:32
To manually set the status here to response using the console is
-
5:36
incredibly powerful, allowing you to interactively explore your
-
5:41
application as if you're inside the JavaScript interpreter.
-
5:47
Now to stop Iron Node, all you do is go back to the command line and hit CTRL+C.
-
5:53
That stops Iron Node, closes the debugger application.
-
6:02
And of course, running your application again everything is back to normal.
-
6:10
Now you know the basics of debugging a node application.
-
6:14
Next, we'll configure our API to use the to do model.
You need to sign up for Treehouse in order to download course files.
Sign up