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 REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Exercise: Dynamic Config" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

Create an extendable configuration file based on the development environment.

Get Unlimited Access Now

Transcript from the "Exercise: Dynamic Config" Lesson

>> Scott Moss: Okay, so I basically showed you what needs to happen here. But in order for me to explain it, I pretty much had to show you. There's no test associated with this, and the exercise is really just to create a configuration. It doesn't even have to look like mine.

[00:00:17] You can create it however you want. I just want you to create a configuration that has a default,
>> Scott Moss: And it extends based off environment variables. So if you wanna look at mine, again, I have a default here. I'm requiring the one that has the appropriate environment. So if it's development or dev, I'm requiring this file.

[00:00:45] If it's testing or test, I'm requiring this file. And then I merge it. So that's step one is create your system for that. You can piggyback my system, that's totally fine. Then the next thing is go replace those places that we saw. So for instance, inside of index.js, replace this with your configuration.

[00:01:02] So in my case, it would be something like this. I would import config here,
>> Scott Moss: config from,
>> Scott Moss: config, it's the index, so that's all I've gotta do. And instead of putting 3000, I would put config.port.
>> Scott Moss: Boom, and now whatever my configuration says the port is, depending on my environment, it's gonna be that now, right?

[00:01:31] And I'll also get rid of this.
>> Scott Moss: There we go.
>> Scott Moss: So that's one place. Like I said, the other place you would want to do this is in db.js, you don't wanna hard code. Mine's already changed. So yeah, you don't wanna hard code that URL in, you just wanna do that.

[00:01:54] I would say for now, you probably only want, there's probably only two databases you really should have. You'll have one for testing and one for development, you should make those separate. That way, cuz at testing, the database gets wiped out every time, right? So if that's wiping out your development database, you'll probably aggravated when you actually go to your app and you're like, where is the C data that I put in there?

[00:02:11] It got wiped out cuz you used it in testing. So whenever you make these configuration files, you can see right here I have a different database for testing than I do for development. Development actually just defaults to this one. All right, the way my system works is, if I don't pass anything onto these files, it's totally fine, because the index has defaults for everything.

[00:02:33] So whatever's not there, it just won't override it. So your system can be as naive, as simple as you want. It doesn't matter, as long as you have the port, the database. And then for extra credit, you can go ahead and put in the,
>> Scott Moss: auth JWT_SECRET, right here.

[00:02:50] So instead of hard coding the secret that was right here, you can use the configuration to do that. That secret would probably be injected in with an environment variable. And then for super extra credit, you can also have your configuration disable auth or not. So I'm like, I wanna run in dev mode.

[00:03:07] And then for dev mode, I'm gonna say, what is it? Let me go back to it. For dev mode, I'm gonna turn off auth, so I can not deal with logging in a user every time. So this one is called disableAuth. So if I were to say disableAuth, and I go back to my config for dev, and I put disableAuth: true.

[00:03:29] And then now, when I run my server,
>> Scott Moss: Cool, so if I run my server now, I try to hit it.
>> Scott Moss: 3000/api/notes. It still breaks because I turned off auth, but I think it's still looking for a user. Yeah, so I turned off auth, but it's still doing the other middleware, which is this one right here.

[00:03:57] It's still doing this. This is undefined right here, so it's still breaking right here. So maybe I'll extend it to also not do that. Let's see.
>> Scott Moss: There we go.
>> Scott Moss: Let's try it again. There we go, yeah, that was totally it. So that's extra credit, is just turn off authentication with that configuration that's there.

[00:04:19] So you can see the power of this. You can just come in like, yeah, for testing, turn off authentication. Or for development, turn it on, however you wanna do it. But yeah, make a system like that. It doesn't have to be intricate, it doesn't have to be perfect.

[00:04:33] I just want you to get in the habit of building your own configuration thing. I'm gonna leave mine up, this one's not gonna take too long. Let's say, it's 3:52 now, let's come back at 4:15. And we'll talk about implementations that you might have done, any fallacies that you might have figured out, or any other optimizations we could have done.

[00:04:54] And then we'll go from there.
>> Student: So you wanna hit just dev.js or prod, or something like that, to show us the example?
>> Scott Moss: Yeah, sure. prod, nothing, cuz I didn't do anything for prod. dev, I just put an expireTime for the JWT.
>> Student: Okay.
>> Scott Moss: I put in secrets, here's my JWT_SECRET.

[00:05:14] And I disabled auth. For testing, I'll just add a database URL that's different. So basically, if you wanna know how it works in my system, you basically create this base config, right? But every single file is the same shape as base config, minus the properties they don't wanna override.

[00:05:34] So if my dev file, all I wanted to do is overwrite the port, that's all it would have. It would have a port, it wouldn't have anything else. Because the way it merges is, this overrides this.
>> Scott Moss: You can also do a dynamic require, although I don't recommend doing that, you can totally do that.