I have heard a lot of great things about heroku when it comes to hosting a node.js project, so I thought I would check them out. I would like to get into writing full stack applications more, and have them up an running on the web. Whats great about heroku is that i have found it easy to use, and has a great free plain, if you are like me and just starting out with hobby apps.
First you will want to make sure you have the following installed:
- node.js, and npm
- The heroku CLI tool
Then of course you will want to set up a heroku account. At which point you should be ready to start making a node.js app that can be deployed to heroku.
I mean something like this:
With my hello world app I started by making a folder.
Then I put together this index.js at root.
The most important thing here is to get the port that is used by herokus severs, that can be accessed by an environment variable. Here I am checking for it via process.env first, and that should work. The process.argv || 8080 deal is more for working locally.
In my running hello world app the package.json looks like this:
With package.json files the name, and version are always required. In order for the app to run it will also need the main property that points to my index.js file. It is also a good idea to declare the version of node that is being used, but as far as I know it is not required.
Make the project a git folder, and make the first commit. Heroku uses git as the source control system, and it should be a git folder before deploying.
There are some more files that may not be required, but you should still know about them, as they might become more important when making a more complex project.
You might want to add some additonal files such as the Procfile, This file should generate when deploying, but you might want to have a copy in the root name space to have better control, mine looks like this for this project:
As you might gather this is what is really used to start the app. When deploying it can be found out by looking at the package.json, but if for some reason you want more control you can have a copy in root and it will be used.
The env file can be used to set environment variables including things like api keys. Of course it goes without saying that anything that is sensitive should not be pushed to any public repo.
I often use a .gitignore file in my projects to at least hide the node_modules folder, in this project I am not using any npm packages, so it is not needed.
Now use the heroku CLI tool to make the app on heroku, and deploy it. First log in, and then use the create option followed by a name that you want the project to be known by on heroku.
If all goes well the project should now be live at https://dp83-hello-world.herokuapp.com/
I really like heroku so far, it seems like it is fairly easy to make and deploy node.js apps with heroku. I will be making this a series of posts on my site here, as this seems like something great to get into, if you have not done so before.