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 "Configuring the Deployment" 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:

The Heroku deployment server is missing some of the necessary environment variables for the application. Scott demonstrates how to configure the server for the correct environment and database URI.

Get Unlimited Access Now

Transcript from the "Configuring the Deployment" Lesson

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

[00:00:03]
>> Scott Moss: All right, so we're almost there. I'm just showing you through all the stuff. I don't want to just be like here's how to do it and then you forget how to do it. I want you to see these problems. So,
>> Scott Moss: And how to resolve them. Great, so now we got that back.

[00:00:22] Can anybody think of anything else that we might need? It's like one big one, a huge one. Maybe I'll just show you. Let me stop this, [SOUND]. And then now, let me try to run this, the API again. Now I'm going to write. I'm just going to write it locally, let's see what happens.

[00:00:40] Boom, what's that? Yeah, no mongo. We didn't tell Heroku that we wanted Mongo, I was going to try to connect with some database. And we also don't have her production database URL. All right, if we go look in our app, there is no production database. There's production.js, but there's no dv.ur, there's nothing here.

[00:01:02]
>> Scott Moss: So it will just throw an error inside of server.js. It was gonna do config.db.url. There is no config.db in production.js. So this will actually just error out. It cannot read .url is one to find. It will get that error. So what we need to do is one, let's create a db here.

[00:01:22] We'll copy this one, go to production, we'll put it here, we'll paste it, and then we need to get rid of this with something else. So to do that, it's pretty easy in Heroku. At least it used to be. Let's check it out now. And we can go to resources, and we can say find more.

[00:01:42] Yes, final resources, and we can go find the closest Mongo thing that comes up. They used to have so many of them. Here we go. Compose, I think they just got bought out by IBM, did they not? Two days ago, $18 a month? That's not what we want.

[00:02:02] We want the free one.
>> Scott Moss: There it is, that's the one we want. So I can just type in this command here, or I can also click a button somewhere. Where's that button? I guess they don't have that button anymore. So I can just use that command and come down here.

[00:02:19] Just type that in, great. So now we have Mongo added and all we gotta do is get that URL. Do I know the URL of this Mongo database? No, no way. But I do know the environment very well because it will tell me if I go read the docs, right.

[00:02:37]
>> Scott Moss: Somewhere here. Here we go.
>> Scott Moss: It will be Mongo lab URI. So that's your URL right there. So I just copy that guy. Then I could come back in here on the production.js, and I'll keep that area as a default. But what I really wanna do is process.env.

[00:02:59] that, or URI. Is that what it was, or URL?
>> Speaker 2: URI.
>> Scott Moss: URI, okay. Yeah, so that's gonna be our URL. Or whatever that is. You see how that works? That's how I get my database.
>> Speaker 2: If it was production, we wouldn't wanna provide it or would we?

[00:03:17]
>> Scott Moss: No, it wouldn't do. It was gonna break anyway. Yeah, it's just still not right, so, yeah I guess you really don't need that there. What I would do is I provider or, and I'd have another environment variable here and another environment variable, like three backup ones. Just in case, but yeah.

[00:03:33] So we also need to verify that Heroku is in fact running in production mode. So what we do is, why did it log me out? I don't even know. Let's go back in. And we can do this because Heroku actually makes it pretty easy. If we go here.

[00:03:54] [INAUDIBLE] If we go click on settings we can look at all our environment variables, right here. So there's our Mongo lab one that we just added. We can edit, we can add more. So let's just make sure, I'm sure it is on by default but let's just guarantee that it's on production.

[00:04:09] So we'll say production. I'll also go ahead and add in my JWT secret. What's the name of that? It's called process.JWT, so just capitalize JWT. I'll call it JWT and it'll be called kittensBacon. [LAUGH]. Kids, it's bacon. All right, so then we got that. Are there any other environment variables that we need to put in?

[00:04:39]
>> Scott Moss: The port's gonna be given to us. And we don't have to, I think we're good there.
>> Speaker 3: On production js?
>> Scott Moss: On production js.
>> Speaker 3: You should remove that or thing.
>> Scott Moss: Yeah, thanks dude. That would have broke. All right, so we'll save that. When you hit save, it'll refresh the server with the new environment rules.

[00:04:58] Yes.
>> Speaker 4: Port's not listed there.
>> Scott Moss: Yeah, it's not listed there, but it's there. Cuz it's a dynamic, it's not a set value. It might be 12345 today, and it'll be 546789 tomorrow, so they just don't list it. Good question. I think that's all the things that we need.

[00:05:19] I can't think of anything else. I honestly think this one's gonna work, so let's see. Now I'm actually trying to make it work. One more thing, one more thing. No, we don't need that. Sweet, just making sure we have our dependencies. So git, what was it? Heroku push?

[00:05:43] Git push heroku, or?
>> Scott Moss: There we go. Git push Heroku master.
>> Scott Moss: And let's see what happens. Yeah, it definitely put it on production.
>> Scott Moss: And then what I'm gonna do is I'm gonna say Heroku logs --tail, which will keep the logs open for me the whole time.

[00:06:28] So that's great. State changed from starting to up. So that's a good sign. When it says up, that means it's great. Haven't seen an error yet. So I'm guessing this is running. And let's check it. So if we go to, where is it at, this guy. Refresh, that's what I'm talking about, looks good.

[00:06:48] 404, that's exactly what I'm expecting to see. Api/users/nothing. Great, empty array. That's exactly right, there's nothing in the database, it should be empty array. All right so now let's add some CRUD to it, you got a question?
>> Speaker 2: There's a question, does the connection between your node app and the Mongo database have to go out through the internet or are they talking to each other via the data centers internal network.

[00:07:13]
>> Scott Moss: Say that question again.
>> Speaker 2: Like the database and the node app, are they talking over a public interface or is that internal to the Heroku's network.
>> Scott Moss: They're talking through the internet cuz the database is hosted somewhere else. So the database that we connected to is through Mongo labs and Mongo labs is hosting that database somewhere else and is talking.

[00:07:32] Yeah it's definitely somewhere else. Horoku isn't hosting that database. They just, like every, the database that we just added, you can go to the website and add it yourself. It's just that Heroku has this marketplace of all these services to show you all this cool stuff. But all it really is, is just a service that you can go get yourself but for free or Heroku, super cheap cuz Heroku makes everything free, except for Mongo, except for compose which is weird, cuz I've never seen anything from Heroku that wasn't free for the base level.

[00:08:02] I don't know how compose got away with that. But Heroku must really like them, but I never seen it. All right, so now let's check it out. Let's grab this URL, and let's do some CRUD on it, all right? So, let's go over here. I'll just, I'll keep this log open so we can see it, and we'll do the CRUD over here.

[00:08:18] So, let me clear the log. It's not, there we go. So http, do I do s? Don't do that, that would be cool, though. So, http, that url/, wait, we need to do a post, right? Let's do a post to that URL and we'll do a, let's create a user.

[00:08:40] So user name equals Scott. Password = 12345.
>> Speaker 3: Do you need to change the route? The users route?
>> Scott Moss: Forgot.
>> Speaker 3: You just had the base.
>> Scott Moss: Thanks, that's probably why it 404ed, api/users, there we go. Boom, got a token back. Totally worked. So now, if I refresh this I should see myself, right?

[00:09:13] Yeah, yeah, yeah.
>> Speaker 3: [INAUDIBLE]
>> Scott Moss: All right, so one more thing I wanted to show you. As far as like managing this stuff, actually no, nevermind. I was gonna show you how to look at the database. If you look at the database, you can click on whatever, in this case it's Mongo lab.

[00:09:33] You can do whatever. You can click on Mongo lab. It will take you to their website and you can go through the database. You can also connect to this database through the terminal, just like we were before. And they give you the command, it says it right here.

[00:09:44] If you type this in right here, it would allow you to connect to the terminal, and you've just got to replace it with your username and password. But I'm not gonna do all that. But then, here's our database, it has one user, which is me. Right there. There's my hash password.