Deploying to Heroku with Node & Mongo
Before You Do Anything
Make sure your app is under version control with
git. If you're not sure if your project is under version control yet, you definitely haven't been committing often enough! Rungit statusto check if your project directory is a repo. If not, rungit initto make it into one if necessary (then commit everything you have so far).Make sure you have an account with Heroku.
Make sure you have installed the Heroku toolbelt.
Add a new remote to your project repository that points to Heroku's servers. NOTE: YOUR PROJECT MUST BE A GIT REPO TO CONTINUE.
In the terminal, from your project's root directory, run:
$ heroku create YOUR_APP_NAMENote: If you don't supply a name for your app, Heroku will create a random one for you. We strongly suggest giving your app a name to personalize it and reflect its purpose.
Also in the terminal, from your project's root directory, run:
$ git remote -vYou should see something like this:
heroku https://git.heroku.com/YOUR_APP_NAME.git (fetch) heroku https://git.heroku.com/YOUR_APP_NAME.git (push) origin https://github.com/YOUR_GITHUB_USERNAME/YOUR_GITHUB_REPO_NAME.git (fetch) origin https://github.com/YOUR_GITHUB_USERNAME/YOUR_GITHUB_REPO_NAME.git (push)
To Start
In the terminal, from your project's root directory, create a
Procfile. "Procfile" should have a capital "P", it should not have an extension, and it should be in the same folder as your main server file (usuallyserver.js).$ touch ProcfileAlso in the terminal, from your project's root directory, run:
$ echo 'web: npm start' >> ProcfileOpen your project in Sublime. In your
server.jsfile, where you get your server started, change theportargument in yourapp.listenfunction so that it looks for aproccess.env.PORTenvironment variable first.// server.js app.listen(process.env.PORT || 3000);
Heroku MongoLab
In the terminal, from your project's root directory, add a Mongo database to your Heroku app:
$ heroku addons:create mongolabNote: At this point, the terminal may ask you to enter a credit card number. Heroku charges for some services or if you go over certain data limits. With the tools you're using and the size of your project's database, everything should be free. Heroku requires you to add a card when you first set up your MongoLab link just in case you eventually go over the free plan limits. If you had to enter in a credit card, run the
heroku addons:create mongolabcommand again.Back in Sublime, add the following to the
mongoose.connectmethod inserver.js:// server.js mongoose.connect( process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/YOUR_LOCAL_DATABASE_NAME' // plug in the db name you've been using );
Dependencies (Node Modules)
In Sublime, check your
package.jsonto make sure all your dependencies are present. If something is missing, install it. For example, run the following if you're usingexpressandbody-parserbut don't have them listed under"dependencies":$ npm install --save express body-parserNow add a
startscript for your application in yourpackage.json:// package.json ... "scripts": { "start": "node server.js" } ...Note: This is assuming your main application file is called
server.js. If your main file is called something else, adjust the script to use your file name.
Deploying
You should be all set up now, so do one more
git addandgit commitbefore pushing to Heroku:$ git status $ git add -A $ git commit -m "ready for first deploy" $ git push heroku masterIf all went well, you should be able to visit your live application by running:
$ heroku open