This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Exercise 4 Solution" 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:

Scott walks through the solution to exercise 4.

Get Unlimited Access Now

Transcript from the "Exercise 4 Solution" Lesson

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

[00:00:03]
>> Speaker 1: This was an example into how to get a little bit into organizing our application using sub routers and sub modules and whatnot in Express. This small layer of abstraction here, so there were some nuances of. Literally copying over lions, actually let me check out this solution. There's some nuances because one, there were some typos in there like what to-dos I thought I'd cleaned those up but I guess not.

[00:00:34] And then there was step four? There we go.
>> Speaker 2: Are we on 5?
>> Speaker 1: Are we on five?
>> Speaker 2: No we're on four.
>> Speaker 1: No four, okay okay. Yeah, there's also things requiring a low dash, and stuff like that that you have to make sure you get right.

[00:00:55] But I think what we can do is let's start at server. And this was already here for you so we're already requiring the lion router so you have to fix, or the tiger router, I'm sorry. You have to resolve that error which means just export something in tigers.

[00:01:10] And then we need to just register it. Doesn't matter what order we put this in, here or here doesn't matter but if we put it up here now we don't get bodyParser, but bodyParser, JSON for tiger route. If we put it up here we don't get these, and so forth.

[00:01:24] Yep.
>> Speaker 3: Just a quick question, as you go through this they were just kind of wondering, can you show how you check the API as you're going to make sure it's working? They're just confused about that. Do you just like, use HTTP pi utility and manually test the results?

[00:01:39] Or.
>> Speaker 1: Yeah, so. Good question. So because there is no UI for it, right. Yeah, so I mainly just use HTTP pi, and I just test it for the command line, so let me see. I'll start it so you can see what it looks like. So I'll say nodemon server server.

[00:02:00] It's all the lines yet. And what is that, port 3000? So now I'll say http port localhost:3000/tigers. And that's how I test my GET request, so this is what I got back. I got back an array. So I mean you could curl, you could use Postman. You can use whatever you feel like I like HTTP pi it works pretty good you can do things like what is it post I think or maybe dash dash post.

[00:02:30]
>> Speaker 2: No post should work.
>> Speaker 1: It's just post okay, yeah then you say what you wanna post like name equals Tony the tiger or something like that and then you can just post things so yeah.
>> Speaker 3: So will you use the postman?
>> Speaker 1: Yeah. I usually use postman religiously but then I just got sick of having to switch from the terminal to another application.

[00:02:52] So I just used this. If I were going to use a gooey, I would use that. That's awesome. Just make sure that when you use postman that you are posting JSON data, and not form data. And it is type of application slash JSON and not the type of text, otherwise you will get unexpected results.

[00:03:10] By default it defaults to form data so just make sure you're not doing that. But yeah, that is how I test an API, it is allot faster me to do it that way. Just foom foom foom. Question?
>> Speaker 3: Is there a way to require a module in your server JS file that way it makes it required to all other required modules?

[00:03:31]
>> Speaker 1: Is there a way to require a module in server.js file and make it available to all? So anything that you attach to global, so that's another variable that we get inside that iife that I was talking about, that this file is wrapped in. Global's another variable. It's an object.

[00:03:49] If you attach anything to it, you can access that in any file without requiring it. So if I said global.config = this object. Now anywhere else inside this environment, I can just type in config, and it will give me that object, even in let's see. This file, or actually, yeah.

[00:04:10] Even in this file, so because it's global, it's in the global space now, so now I have access to it everywhere after this. So now if I go to like lions.js if I looked at config it would be the same object.
>> Speaker 1: It wouldn't throw an error, so I can say config or I can say global.config whatever I want to say but either way.

[00:04:32] Global is like the window of node. But it's like an anti-pattern. Only time I've seen global work and was okay with it, was with sales. Sales JS uses global to set up config and models, and stuff like that. And I kind of liked it there. But other places, not really a big fan of it because then you start getting lazy and you're like, I was gonna put low dash on global.

[00:05:01] And I was gonna put everything that I required twice on global so I don't have to require it twice anymore. Like, next thing you know you have a file that's like Tint that's got like a hundred requires in it. That's all on global. It's like what's the point of commonJS now?

[00:05:14] I don't recommend it, but yeah you can do that. That's the only way to do it. And that's because again all the files are held in the closure with an iife.
>> Speaker 1: Cool, so yeah, we register the tigers. And then over here like I said, it's the same crud.

[00:05:38] The only difference is you just do a change of tigers, or lions to tigers and I have some to do's here and change those. And then, I think there was another error with like,
>> Speaker 1: This require not loading up. Or for the low dash. And also, there's one more thing.

[00:05:52] If you go to server.js, inside the error handling, this is gonna catch like the error, so if you don't log the error, you're never gonna know. If your server breaks, it wouldn't tell you, your server would just break and you wouldn't even know it happened. So you should probably just log this.

[00:06:11] And you can say error.message. So that will log the error, so now you know what it says. Yes?
>> Speaker 2: That same send error there, is that correct? Or should it be send error?
>> Speaker 1: There might, is there a send error?
>> Speaker 2: Well I mean, no I'm sorry, I meant the parameter is spelled out error but I-

[00:06:28]
>> Speaker 1: Thank you, yeah, yeah. Sorry. Yeah, you're right. That carried over from the last branch, yup.
>> Speaker 1: I thought you meant there was a send error. I was like, they added that now, that's legit, you can make it. You can modify the response object, too. If you wanted to, you could make a middleware that's like, yeah, app.use, make a middleware that creates a resonse.syn error.

[00:06:52] And it automatically knows what to do. So you could call that anywhere. You can do that too