Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Error Handling" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Before moving on, Scott makes a few code optimizations to demonstrate who to simplify the routing syntax. He also talks briefly about error handling. Node doesn’t output any error details by default so Scott adds an error handler and explains how to use the Node debugger/inspector.

Get Unlimited Access Now

Transcript from the "Error Handling" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Speaker 1: All right, so before we move on, I'm just going to make a small optimization to show you guys. Now that we have these mini routers, and we could have done this on the app but I think it makes more sense to do this here, is in places where we have the same URL like, /:id', /:id, /:id, we have that /:id three times and then we have root root twice.

[00:00:24] I can clean this up a little bit by using the .route method. And that exists on an application instance and a router instance. So I can say tiger. Oops, sorry. Tigerrouter.route. That's a function that it takes in a path. So in this case, the route and then like I say, on dot get just do this.

[00:00:55]
>> Speaker 1: Do that on dot get. And now I can just get rid of that. And then on post, do this.
>> Speaker 1: And I can get rid of the post. So I'll come back and say .post and put that in there.
>> Speaker 1: I can do the same thing for the other route which is ID.

[00:01:21] So tigerrouter.route/id which has got like three of them on it right? And that's got a get which is this one.
>> Speaker 1: Get rid of that.
>> Speaker 1: And it's got a delete, which is this one. Get rid of that.
>> Speaker 1: And then one more which is the put. We can get rid of that.

[00:02:07] So just cleaning up some code same functionality because they share the same route path. So I can just use the dot route method, so it's only route the route wants. A little bit cleaner, so you can also do this on app. So if I went to app, I can also say app.route.

[00:02:26] It's the same thing, they work the same way on routers. So again, I'm saying, so if any request comes in for route, and it's get, run this. And if it's post, do this. If any request comes in for route/id, and it's a get, do this. If it's a delete do this.

[00:02:43] If it's a put, do this. It's just a shortcut. So we saved a little bit of keystrokes there. Any questions on that? I'll go ahead and push this up to [INAUDIBLE] solutions. So if you want to pull it down.
>> Speaker 2: There's a question I'm kind of finding tracking down some errors.

[00:03:17]
>> Speaker 1: Yes.
>> Speaker 2: They're asking how do you track down and report an error on an undefined method, for example if you didn't require lodash in the lionRouter, you used get request lion will fail but the console just indicates a 500 error?
>> Speaker 1: Yeah, so to fix that you have to come down, so I said in server.js.

[00:03:40] You need to make sure you log your errors here. That's why that wasn't working. It would show the error like couldn't find module load [INAUDIBLE] or something like that. So if you were to just log this error.message that's gotta fix that. So by default, it will show that.

[00:03:52] That's Express taking those synchronous errors and it's just not doing anything with it, so yeah. Just log your message here. But if I had to find, like track an error down, this is a good entry into like debugging in general with node. So let's just talk about that for a second, it's worth mentioning.

[00:04:12] There is a node debugger.
>> Speaker 1: What? What? Is it node? Debugger. I forgot, I haven't used this this a while and don't know what, I this is node, debugger. No, wait. No debug, debug. There it is. Yes node, debug, server server. That's just going to run your code in debug mode.

[00:04:44] It'll immediately stop at the first line. And this is kind of hard to use, like you can step, and go to the next line, and stuff like that. And it's all through this terminal interface. This is okay. I don't know anybody professionally that uses this. I cannot recommend it but if you wanted to, you can get busy with it.

[00:05:05] What most people use, if they use a debugger at all, would be node-inspector. If you want to use this, then you need to npm install globally node-inspector, it's a global thing. And a note inspector will take the path to the file. And what happens is, if you run that command it'll give you this URL to hit in your browser.

[00:05:30] So grab this. Go to Chrome. Paste that in. And what happens is it will load up. Or it's supposed to. It looks like it didn't really load it up. It's supposed to load up, so if you see it looks like Chrome developments was here that will run your node GS through Chrome development and have the same stuff.

[00:05:48] Not sure why it's not loading, it loaded up the GUI but it's not actually loading up my files and stuff. Let's see, no this isn't loading up either. So yeah, if you do choose to use it, then you would need to use Node Debugger. Me, personally, I'd just cancel out everything, and I've been doing that for like two years node, and I haven't had a problem with it.

[00:06:11] So, it's a smart thing. To me, it's just faster. But yeah, there do exist Node Inspector that will do this. This is not part of node, it's a third party module that's supported, so yeah. It's using web sockets to do debugging for you, not sure why it's not working.

[00:06:31] I think it has something to do with the fact that we're trying to debug an express server, not a static file. I'm sure there's a different flag I need to pass in for that. But like I said, I don't use debugging. I just console.log everything.