v0.0.5 Deploy to Heroku - zhentian-wan/MEANAppsFiles GitHub Wiki

Few things to do before deploying

  • add node and npm version to the package.json:
  "engines": {
    "node": "0.10.x",
    "npm": "1.4.x"
  },

If you don't know the version, you can type in cmd node --version and npm --version.

  • Add Procfile Procfile is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform. see
web: node server.js
  • Heroku app run on port 8080, but currently we run on local 3030, we need to update this part of code in server.js and use process.env to help us:

    var port = process.env.PORT || 3030;
    

MongoDB for Heroku

  • We need to add MongoDB adds-on for heroku to enable us using MongoDB. Go to Heroku adds-on page to search the adds-on you want to use.

  • Here we use MongoLab, First you need to create a account for MongoLab. We select free one.

  • Then create a user for MongoLab, and you also can find the connect information.

  • Because there is no data in the MongoLab, we can connect the MongoLab using cmd and insert one data:

    db.messages.insert({message: "Hello MongoDBLab"})

  • We need to update our server.js, because it use 'development' env, when you deploy to the heroku, we consider this is 'production' env.

//Use Mongoose
if(env === "development"){
    mongoose.connect('mongodb://localhost/multivision');
}else{
    mongoose.connect('mongodb://<username>:<password>@ds031932.mongolab.com:31932/multivision');
}

Deploy

Use Git Shell

git s
git add -A
git commit -m "Ready to deploy to Heroku"

heroku login

//create heroku
heroku create
//list the remote and see
git remote -v

//set production env
heroku config:set NODE_ENV=production

//push code to the heroku
git push heroku master

//scale you app
heroku ps:scale web=1

//open you app on heroku
heroku open

About scaling

Heroku Troubleshooting

One useful command:

 heroku logs

It shows your the log about every steps. It is very useful when some problem happened. For example, my package.json missing some npm packages, it will tell you what package cannot be found, what you need to do is add some packages into the package.json.

About update you dependency.

⚠️ **GitHub.com Fallback** ⚠️