Check out a free preview of the full Introduction to Node.js, v2 course:
The "Deployment: Servers" Lesson is part of the full, Introduction to Node.js, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates how to deploy the server application to Heroku. When deploying to any hosted service, a 'start' script should be declared in the package.json file so the server knows which file to start. Any hard coded server settings are replaced with environment variables.

Get Unlimited Access Now

Transcript from the "Deployment: Servers" Lesson

[00:00:00]
>> Now, when it comes to servers, servers are API's, they need to be hosted on a machine just like they're hosted on your local host. They have to be hosted somewhere else. So there's a lot of places you can deploy servers. We could talk for days about which one's better?

[00:00:15] Which one's this or that? But at the end of the day, they're pretty much all the same. They have different trade offs. We're going to use Heroku today because it's just the easiest to do. It has the simplest UI. And you don't need to be a DevOps person to understand half the options in the drop down.

[00:00:30] So we're gonna do Heroku. If you've never heard of Heroku, the best way I can describe Heroku, it is a really nice UI on top of AWS. They use AWS as their infrastructure as do a lot of other competitors to Heroku do. It's just a better UI on top of it with better features.

[00:00:52] Well, I wouldn't say better features, simpler features. It's an abstraction around the complicatedness of AWS. So we're going to use Heroku for that. And to get started, we just have to do a few things and already done all these things. So I'm just gonna walk you through some of the things that I didn't do and then we'll get to it.

[00:01:10] The first thing is we need to get our application ready to be deployed. It has to be deployable on another machine. Right now is a lot of stuff that's hard coded that only works on our machines. Specifically like the port, we hardcode the port to be like 8000.

[00:01:27] That's not gonna work. When we deploy this, we have no idea what the port is going to be. It's going to be assigned dynamically to whatever Heroku wants it to be. So we have to find a way to make that a dynamic variable because we don't have access to it yet.

[00:01:39] So those are some of the things we need to do. We also need to teach Heroku about what type of app this is and how to start this app. You're not always going to have the same start script. Sometimes you need to do this or do that. So you have to specify Heroku how to start it.

[00:01:51] And then we actually have to create a Heroku app and push it up there. So we're going to do just those things. The first thing, like I said, is create a Heroku account. If you don't have one, it's free, it doesn't cost any money to create an account.

[00:02:03] They're pretty generous with their free tiers. So highly recommend creating a Heroku account. It's great for just prototyping servers that you want to deploy. Unless you're really serious and you're making money. Yeah, you can just get away with a free account for a long time. And then you want to install their Heroku CLI.

[00:02:21] This is just one way to create an app and deploy it. So we're going to follow this path. But you can also just go through their GUI, do a couple clicks and get it deployed that way. But for the sake of node.JS, we're gonna stay in the terminal.

[00:02:32] We're gonna try to do it this way. So, install their CLI. And once you have that, you just need to tell Heroku what type of app this is. So, the first thing we can do is we can add a start script to our scripts inside of our APIs.

[00:02:47] So, I'm gonna go to our API here, which is going to be the server. We're going to go to the package.JSON that I don't have. So I'm going to make that. And I'm going to say NPM. And it's like that. In the server, there's one more. Forgot that last enter.

[00:03:14] So we're going to go here, we're going to go to scripts. I'm going to do start. And I'm just going to point this to the entry to my server. Which in this case is going to be, I want to run the express server which is server.ms. So I'm going to point to that.

[00:03:30] So I'll say node, Server.mjs. So we're gonna point to that. So that's how you start our server. So now Heroku knows how to do that. And the last thing is I need to tell Heroku what version of JS that I'm using. And this is important because we are using a relatively new version of JavaScript that supports modules.

[00:03:49] So I need to say, engines and then you can say node. And then you could put the version. Mine is 14.17 or what version am I using? Let me see node. Version 14.170, you could probably also do literally the number 14. It will probably work as well. It'll just find the latest 14.

[00:04:15] So you wanna do that just to specify we're on the same version because the last thing you want is like well, it worked on my machine. Well yeah your machine's using version 14, but on Heroku we're using version 8. So yeah, I'm sure it did work on your machine.

[00:04:27] It's a common thing. This is why something like Docker was created to help with these problems. But we're not using Docker, we're just going straight to Heroku. Okay, so now that we got that, we do need to clean up our code a little bit to not be so hard coded.

[00:04:41] So the one specifically is this port. Instead we need to make this dynamic, it needs to be a variable. So we're gonna say process.env.port capital P-O-R-T. Now, I know this to be true. Not only because I've deployed to Heroku so many times, but because this is obviously the default environment variable that every single hosting provider uses.

[00:05:00] They use port, all caps. So we're gonna do that and I'm not gonna put local. I mean, we can leave this here, this log is fine. It's just it's not gonna be accurate because we're not on localhost 8000 anymore, but you should change that as well. And I'm sure Heroku does inject like a host name variable on the environment that you could put here if you wanted to, but it's not hurting anybody.

[00:05:19] So we'll just leave it. And the next thing we want to do is we have the Heroku CLI, we should say Heroku login. Okay, press any key, okay. And they want me to go update my Terms of Service on Heroku. So I guess I'll do that. Got this password manager here.

[00:05:47] I don't think I have MDM here otherwise I would have used it. So [SOUND] I want to use this accounts. Okay, let's do it. We're already here. Okay, looks like I updated my stuff so we're good here. The next thing I want to do is let me just date this along.

[00:06:13] I want to make sure I have the proper GitIgnore. I don't want to check in these node modules. So I'll do a dot.gitignore and at the very least, I'll check out these node modules. But you should also check in pretty much everything I have here. Very helpful, according to Heroku.

[00:06:34] So but yeah, you don't want any error logs, you don't want DS store which is like this floating ghost file that floats around on Mac. It's kind of annoying. It's really big. Any .ENV files which are probably full of secrets, so ignore all those. And we did the login.

[00:06:51] Now we have to do a create. This is going to create an app on Heroku. Like I said, you could also do this in a GUI. It's totally fine if you wanna do that. I was gonna do it here from the command line. I'm gonna say Heroku.create. It's gonna make us a nice app, get that nice Heroku spinner, that nice zen name hidden Savannah.

[00:07:09] That's our URL there. And I need to add this Git URL to my Git repositories. So this is not a Git repository yet. So I'm gonna say Git init like this. And then I'm gonna say Git remote and we'll call this Heroku. And we'll paste that to there.

[00:07:36] So now that I got that, I can say Git Push Heroku name. Did I not? Wait, did this already do this? Let's check this out. So, wait hold on. Lots of additional rows first. Is it not called name? Hold on, let's see Git remote dash v. Yeah, it's right there.

[00:08:05] Git push Heroku, maybe they still call it master? No. Good push for Heroku. Let's say git push Heroku. I want to branch. I'm pretty sure it's master or main. Or yeah, git push for Heroku. Guess it doesn't like that I did that. Let's just create a new hat.

[00:08:33] And I think it'll set up for us now that I have a git okay? Now let's look at git remote-v. Or gits remotes Heroku remove, git remote remove Heroku. Now let's create another one. And hopefully it added it for us. There we go. So it looks like it added it so then I'll say git push, Heroku.

[00:09:07] Maybe that was it. So let's try to commit some code. [SOUND] I thought it will still try to push. [SOUND] Whoa, what happened? Whoa, sorry. I don't know. What happened to my terminal, okay? Open it back up. What? Okay, that was weird. That was really weird. What was that?

[00:09:46] Okay, let's try to push this now. Now git push Heroku okay, let's go to the GUI there. This is why you have a GUI. So, let's go to Heroku. And I have a bunch of apps in here somewhere, let's see. Personnel looks like it was all of these right here.

[00:10:20] So I'll take this hidden Savannah one. And I'm going to deploy and I can deploy from this thing says let me try this. Okay, looks like we're good here. I might actually be able to do this. Let's give that a try. Hey, look at that. I love that copy and paste documentation.

[00:10:49] So as you can see, it's building. This is the output of the build. It's transferring to Heroku and is trying to detect what type of app I have and all that stuff. So it says, Node JS app detected. Here's some environment variables that we put in your app.

[00:11:05] Installing these binaries [SOUND] a bunch of stuff here. Build's succeeded, that's what I wanted to see. Default types is the web process. So in Heroku, a web process means this is an API. Because you can also have background, job processes, which are background jobs. And that web process defaults to web if you have a start script.

[00:11:27] And it says it deployed. So let's see what happens. So if we try to go to this URL, let me just confirm what we had in our API. Which was a slash to do post and Git. So I'm gonna try this from the terminal. I'm gonna say HTTP.

[00:11:46] That thing slash to do still get requests. And look, it returns an empty array. That works. So let's try to do a post and put a something in there. I think it was a text field we wanted on the object. So we'll say a clean kitty litter. My cat poops a lot, so we got to clean the kitty litter.

[00:12:08] And we're gonna do that, looks like that worked. We can get it back with this and boom, there we go. Our app is deployed, we can confirm. If we go back to Heroku and I could look at activity or not activity, I'm sorry, I should be Vlogs here we go.

[00:12:23] And we should see some logs here. You can see that I did a get request to slash to do. I did another post and another Git here. So everything seems cool. And then here's our outdated localhost 8000 log, which isn't relevant anymore. But like I said, it's not hurting anybody.

[00:12:37] And that's how you would deploy a server. So Heroku allows you to add databases and different things like that. And they have documentation on that much better than I could describe. So if that's what you're looking for, you can go much further with that.