Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Deploying to Heroku" 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 will be deploying the blog application to Heroku. He logs into his Heroku account and creates a new application. Scott then demonstrates the process of pushing the application to the deployment server and debugging any issues.

Get Unlimited Access Now

Transcript from the "Deploying to Heroku" Lesson

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

[00:00:03]
>> Scott Moss: Like I said, I'm just gonna go through deployment, I think it's better. Like I wasn't gonna just have notes and be like this is how you deploy, and this is it. But it's so much better, if we just walk through it. Especially, if you want to follow along on your computer or just watch me go through it and watch me stumble through all the nuances that everybody goes through.

[00:00:19] Because that's just what happens, so let's do it, let's just go to Hiroku. And especially if you've never deployed on Hiroku before, I think this will be great to see somebody who thinks they know what they're doing, how to do it. So, that'll be really cool. So let me see, what was, don't be trying to count my password length.

[00:00:39] I see y'all over there. [LAUGH] So, let's just make a new one. We'll make a new app, and we'll call it ngblog api. Only lower case? Okay. We'll use this convention then, there we go, create app. And Heroku actually kind of upgraded, now that they've like integrated with GitHub, so you don't have to do anything anymore.

[00:01:09] Before you had to like do some stuff, but now it's just like [SOUND], just attach it to GitHub, push it, it's good. Which is like, everybody else does, so I don't know why it took them forever but, they got it, they got it. So now you just like, first thing you need to make sure you have this Heroku tool belt.

[00:01:26] So I need to do that, I know I have it but I just need to make sure I'm logged in. So I'm just gonna say Horoku. What was it? Login.
>> Scott Moss: There you go.
>> Scott Moss: Okay cool, so now we're logged in
>> Scott Moss: Okay, stop doing that. Here we go and then need to add the remote URL for Heroku.

[00:02:05] Our version of git is so out of date. I thought I updated that, guess not.
>> Scott Moss: I went into Heroku's office a couple weeks ago. They have a really nice office. I don't know if anyone's ever been there? It's like ridiculous. It's like bamboo everywhere. Everything's bamboo. It's really nice.

[00:02:24] They have this nice rooftop room where they party at. It's really nice. All right, so now we've got that, and we can verify it if we type in get remote/v well see that look there's a remote url by the name of Heroku, and it's our api. So we got that, tada, and let's just go ahead and make sure I added commit it here, get diff.

[00:02:53] That's not important, let's just stash it. Great. So, we have a clean thing here, and all I have to do is just say git push heroku master. Now who thinks here, if I push this heroku, is this gonna work the first time? Right? It's just gonna work the first time.

[00:03:08] Right? I know it's gonna fail, but I want us to see the error, so let's just do it. What is it, push it, push it? Yeah.
>> Scott Moss: So, let's just push it. I just want you to see the logs. It's like, Node.js app detected, great. Here, and then it injected these environment variables into our app.

[00:03:30] And then it's like, okay. He didn't tell me what engine you were running, so I just figured it out. I'm restoring any cache that was there from last deployment. The npm installed some stuff, and now it's attempting to launch the app. And it said it did it, it did it, it totally did it.

[00:03:50] So, now if we go to Heroku Open.
>> Scott Moss: Application error, looks like it crashed. So, what we can do is we can say Heroku logs//, or we'll just do logs. Let's see what happened. So, it went in here. It ran node server.js. We'll that's not the command I wanted it to do.

[00:04:23] That's probably why it crashed. It should be index.js. So, It broke right there. Why did it do that? Why did it run server.js? That's because
>> Scott Moss: I don't know why it did that to be honest. [LAUGH] I would have thought it would have ran this, because it's supposed to run npm start but, I guess not.

[00:04:44] But yeah I don't know why it did that. So what we need to do is we need to make a new file on here, on the root. And we'll call it proc, P-R-O-C, file. This is something Heroku wants. And what we can do in here, we can say web, and then give it the command that we wanna run, index.js, I think that's the format that it wants.

[00:05:06] I don't know, I think that's the format that it wants. I might have to look at it. Let's look at it. See, procfile.node.js, Heroku.
>> Scott Moss: Yes. What's the Procfile, guy? Yeah. That was it. Yeah, so. Cool. It, it says it defaults to nodeserver.js, if nodescripts.start entry is present in the server, yeah.

[00:05:44] It's cool. Okay. So, I'll just say node index.js instead. That's step one. Got that out of the way. Another thing I was freaking out about if people look at the output. It'll send something about, couldn't find an appropriate engine or something like that. Where did it say that?

[00:06:02] Did I pass it? Wait, hold on.
>> Scott Moss: Here we go. Engines.node unspecified, engines.mpm unspecified. So, you can tell it which version of node you want to this to run in. By default, it uses the most recent, which is 12, so we don't have to put anything, nut what that means is come in here and say engines.

[00:06:23] And you can say, node. And then you can put what version of node you want it to run, but I'm not because this is going to run the latest, so I'm fine. So, we got that. Okay, so now, if I deploy this, is it going to work now?

[00:06:37] Everybody think so? I didn't see any other errors. Let's check it out.
>> Scott Moss: Cool, so the status, git add --all, -am'chore', okay. And then let's do get push heroku master. Everything up to-date. I know why, I know why. Heroku pushes the master branch be default. We're on step 12 fix.

[00:07:11] That's why. That's why it's freaking out. It's deploying the master branch. Doesn't matter what branch we're on here, it'll deploy the master branch, which is really weird. So we need to git check out master, and then git rebase step, or git rebase origin/step-12-fix. Okay, I think that's good, let's check it out.

[00:07:42] Is that the right thing? Yeah.
>> Scott Moss: Or no, hold on. Let's see. I need to push this up step-12-fix.
>> Scott Moss: All right. And let's check out the master. Pull. Come on. There we go. You know exactly what I want, I don't know why you're tripping. Let's just do a rebase.

[00:08:35] Okay, I think we're good now. Yay, we're good. Okay, it even gave it a little heroic logo. That's dope. All right. So, now if we push at Heroku, let's see what happens. And I knew that I needed a master reg, because it said everything was up to date.

[00:08:56] I knew everything wasn't up to date, because we just made a whole bunch of changes, that's how I knew it was on a branch that there was some weird stuff going on. I remember, I ran into that error or that problem like 30 times. I did, it only does the master branch my default.

[00:09:10] Watch this thing break on the, on the decrypt. Watch. Watch it break. Then we'll just be in the same problem you were on Heroku. Then we'll just give up.
>> Speaker 2: [LAUGH] Where you blame Windows again.
>> Scott Moss: I don't know, I don't know if it's gonna do it, let's see.

[00:09:25] It better, this things like on a Linux Box somewhere. We'll let that build.
>> Speaker 3: Somebody's asking if you use git-flow?
>> Scott Moss: Yeah, git-flow is awesome. For some reason, I don't have it in this environment. I messed up my Git. I deleted it and put some other thing there.

[00:09:46] Git-flow is great. I like git-flow. But then again, I'm not that good at Git either, so, I don't know I might just start using GUIs now. I have like a few, I think I have like tower, do I? Or no? Is it called tower? I don't know. The Git help one is okay.

[00:10:08] But yeah, there's better ones out there. Alright, looks like everything's working so far verifying deploy. It deployed it, now it's trying to check it to make sure it did it. All right, looks like it did something. So before we go look at it let's go look at our dashboard here.

[00:10:26] Just refresh. And let's go to activity, yeah looks like this guy's been deploying stuff. Looks like me [LAUGH] He's been doing some work in there, obviously he's earned this keep. So, let's check it out. So, let's say heroku open, again, that's gonna open us up. Another application error, so you can say Heroku Logs, and let's see why.

[00:11:02] Let's see what happened.
>> Scott Moss: Started process with command npm start. So, it freaked out on npm start. Why did it run npm start when I told it to do something else? Not sure why it ran NPM start, but that's an easy fix. Just come here and be like yo, just do node man, that's all I want you to do, don't do nothing else.

[00:11:31] So that'll fix that. Another quick thing we can do is instead of having to deploy every time, we have this other thing that we get from Heroku called Forman, which is a local, It's like how your output runner heroku if it was on heroku. And we can just say form in start, and that's gonna start our app on port 5000, and it's gonna run it like heroku would run it.

[00:11:54] So, if you go look on port 5,000, can I get of course, because we didn't have a request for that. But, if I say what about api/users, can I get some of that? Yeah, you can get that. So our API's working, that's great.