DEV Community

loading...

Deploy your app with Heroku

Klaudia
Software Developer.
Updated on ・3 min read

I am not deploying my apps very often (they usually end up almost finished somewhere in my local hell), so every time I find myself with the task to do so, the fairly simple process turns into the research and remembering commands I used in my previous lives. Hence, this guide.

I will show the process of deploying an Express app using MongoDB with Heroku step by step.

Prerequisites

First, to deploy an app, you will need an app. I have one fairly simple REST API on my github already which we can use for this tutorial. You can find it here. Simply fork it and make local copy so we can make changes to it later.

To use Heroku, you will need git and heroku. I'm assuming we all have git on our machines so the next step is to install heroku CLI if you don't already have it. On Mac machines, simplest way is to install it with brew (for different OS refer to heroku docs )

brew install heroku/brew/heroku
Enter fullscreen mode Exit fullscreen mode

You will also need to create an account at Heroku website (free for up to 5 apps).

Prepare your repo

Now, if you have a copy of the repo, we need to create Procfile. Procfile is a heroku specific file where you define commands that will run on your dyno. It should be called Procfile (no file extention) and should be localted in your root directory. I usually add one command to my file, using forever package which basically restart your app in case it crashes. Simply run these commands in the root directory of your app:

> npm i forever --save
> touch Procfile
Enter fullscreen mode Exit fullscreen mode

Now, we need to add this to your Procfile.

web: ./node_modules/.bin/forever -m 5 index.js
Enter fullscreen mode Exit fullscreen mode

Commit all your changes before moving on to the next step.

Create heroku app

Heroku docs explain this process quite well and if you find any issues, I would recommend referencing them. But to put it simply, run the following command in your terminal.

> heroku create

Creating app... done, ⬢ afternoon-dawn-70065
https://afternoon-dawn-70065.herokuapp.com/ | https://git.heroku.com/afternoon-dawn-70065.git

> git push heroku master
Enter fullscreen mode Exit fullscreen mode

This created the app and assigned it a name and URL, however, we still need to enable MongoDB.

Install mLab add-on

Heroku offers free add-on for MongoDB and it's fairly easy to implement to your app.

In index.js and also in db/seeds.js, we need to add MONGODB_URI to mongoose connect function, so I changed it to look like this:

mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/polling');
Enter fullscreen mode Exit fullscreen mode

Also in index.js change the port declaration on the last line to:

app.listen(process.env.PORT || 3000, () => console.log('Server has started'));
Enter fullscreen mode Exit fullscreen mode

Commit your changes and push them to heroku ( git push heroku master ), then we can add mongolab with this command:

> heroku addons:create mongolab

Creating mongolab on ⬢ afternoon-dawn-70065... free
Welcome to mLab.  Your new subscription is being created and will be available shortly.  Please consult the mLab Add-on Admin UI to check on its progress.
Created mongolab-deep-94113 as MONGODB_URI
Use heroku addons:docs mongolab to view documentation
Enter fullscreen mode Exit fullscreen mode

Populate your database

I created the seeds.js file which populates your database with dummy data. You can run it and populate your Heroku database like this:

> heroku run node db/seeds.js 

collections dropped
... some not important warnings, bla bla
Candidates created.
Users created.
Done!

Enter fullscreen mode Exit fullscreen mode

And now, if you go to https://your-app-name.herokuapp.com/api/users or https://your-app-name.herokuapp.com/api/candidates you should get a response (JSON with user and candidates info).

So yeah, it is as simple as that to deploy your app, it's just harder if it's your first time or if you haven't done it in a year or so. Thank you for reading!

Discussion (8)

Collapse
thebouv profile image
Anthony Bouvier

I love the simplicity of Heroku.

But find the cost spikes high outside of the simplest applications.

Even for minor MVPs it just becomes too costly for things that are bootstrapped financially.

And then for larger projects, it gets even crazier unless your pockets are deep and no one is paying attention.

Collapse
effingkay profile image
Klaudia Author

I see your point and agree, Heroku can get pretty expensive pretty fast, however, I think it's a great tool for beginners who want to deploy their apps, to showcase in their portfolio for example. I remember I had all my apps there when I started with coding and never had any problem with the cost. Even now, for a simple API, Heroku is my first choice.

Collapse
yechielk profile image
Yechiel Kalmenson

Yup, my entire portfolio (including my portfolio site itself) is on Heroku.

Collapse
heyay profile image
Ayush

Heroku is ❤

Collapse
ben profile image
Ben Halpern

Longtime happy Heroku user here. Nice writeup.

Collapse
effingkay profile image
Klaudia Author

Thank you! Glad you liked it

Collapse
phillie profile image
Philly

"(...) they usually end up almost finished somewhere in my local hell (...)" Yeah, mine too. 😁

Great writeup. 👏

Collapse
effingkay profile image
Klaudia Author

Heh, I'm glad I'm not the only one who abandons my apps!

And thank you!