Skip to content

Instantly share code, notes, and snippets.

@bhuvidya
Last active May 15, 2020 01:07
Show Gist options
  • Save bhuvidya/f796870c3e9770cbaa066583f262cbc3 to your computer and use it in GitHub Desktop.
Save bhuvidya/f796870c3e9770cbaa066583f262cbc3 to your computer and use it in GitHub Desktop.
Setup your macos dev machine to do local development on a Heroku nodejs/parse/mongodb application.

Local Dev Setup Heroku / Nodejs / Parse / Mongodb on macOS

I recently started doing some collaborative development work on a Heroku nodejs application that uses Parse Server with MongoDB. Even though at times it's important to share an online database for testing etc, I found I really wanted to recreate the setup locally. This would mean not having to git push heroku master every time I did the smallest code update, waiting a minute or more for the application to be compiled and deployed on heroku. As a corollary, I can do my work locally in an isolated feature branch, test locally, then merge and push when happy.

This document outlines how I set things up on my Macbook Pro, running Catalina 10.15.4.

Git

You probably already have it, but if not I use homebrew

$ brew install git

More instructions at https://gist.github.com/derhuerst/1b15ff4652a867391f03.

Nodejs

I install nodejs via nvm. Full instructions for macOs can be found at https://www.codementor.io/@mercurial/how-to-install-node-js-on-macos-sierra-mphz41ekk. Otherwise refer to the README of the nvm repo: https://github.com/nvm-sh/nvm.

Heroku CLI

Again, I use brew:

$ brew install heroku/brew/heroku
$ heroku login

but for more info go here: https://devcenter.heroku.com/articles/getting-started-with-nodejs?singlepage=true.

Then you can clone your online nodejs app:

$ heroku git:clone -a APP-NAME

Then you can do a local install of the node modules:

$ cd APP-NAME
$ npm install

MongoDB Community Edition

I follow the instructions at the mongodb docs site to do an install - https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/. (For Windows, https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/).

Basically:

$ brew tap mongodb/brew
$ brew install [email protected]

The above mongodb doc pages also show how to start mongodb as a service etc. I use the approach that the parse-server docs use (see https://docs.parseplatform.org/parse-server/guide/)

$ npm install -g mongodb-runner
$ mongodb-runner start

Parse Server

Because the Heroku nodejs application already has parse-server as a node module, I don't need to install parse-server as a global, standalone node app. But if for some reason you do need to, see https://docs.parseplatform.org/parse-server/guide/.

Parse Dashboard

The Parse Dashboard is a great way to browse and manage your mongo databases. To install locally follow the instructions at the github repo https://github.com/parse-community/parse-dashboard.

$ npm install -g parse-dashboard

Then I create a parse-dashboard-config.json file with connection information. The basic format is:

{
  "apps": [
    {
      "serverURL": "http://localhost:1337/parse",
      "appId": "myAppId",
      "masterKey": "myMasterKey",
      "appName": "MyApp"
    }
  ]
}

Then I run the parse-dashboard web server using:

$ parse-dashboard --config parse-dashboard-config.json

You can then access the dashboard in your browser via http://localhost:4040/.

Transfer Online Mongo Database Data to a Local Database

When you installed mongodb locally, it also installed some utilities, which we use here. The basic process involves getting the mongodb server information for your app on Heroku, and then using that info to dump the online database to a local folder, then restoring that dumped data to a local mongo database. Full info can be found here: https://inphamousdevelopment.wordpress.com/2015/06/07/cloning-mongo-database-from-heroku-to-local/.

So first of all we do:

$ heroku config --app APP-NAME

And this gives us something like:

=== my-app Config Vars
APP_ID:                my-app
MASTER_KEY:            secret-key
MONGODB_URI:           mongodb://heroku_j5th7r5:[email protected]:53729/heroku_j5th7r5
NEW_RELIC_LOG:         stdout
PARSE_MOUNT:           /parse
SERVER_URL:            http://my-app.herokuapp.com/parse

The juicy info is found in the MONGODB_URI variable. This gives us user, passwd, domain, port and database name for the Heroku-assigned mongo database.

Dump the online database using:

$ mongodump -h <url>:<port> -d <database> -u <username> -p <password>

This will export into ./dump/<database>. Make sure mongod is running, then:

$ mongo <local-dbname> --eval "db.dropDatabase()"

where local-dbname is your local database name.

Then do a restore of the local database from the dump files:

$ mongorestore -d <local-dbname> dump/<database>/

And you should have the full database locally now.

Run the Heroku app locally

Change into the heroku app folder created when you cloned the online app with heroku cli (see above). Then

$ npm install
$ npm start

This will start your app, kicking off parse server, which in turn connects to your local mongo database. Make sure the default app ID, secret key, and database names/urls match up with mongod configuration (the default local URLs and ports all seem to match out of the box, you generally just have to make sure AppID, Secret Key and mongo db names all match between the heroku nodejs app configuration, and your local mongo db, and your parse dashboard config file.

Normally, as you edit the Heroku app js files, you would need to kill and restart the npm start process in the heroku app folder. But I came across this amazing npm tool called nodemon. SO install it now:

$ npm install nodemon -g

And then all you need to do is:

$ cd APP-DIR
$ nodemon index.js

and when you edit the js files, nodemon will automatically reload the application. FUCK YEH.

So test your app any way that suits. Then when you are happy with your local code changes, do your usual git commits etc, then the familiar:

$ git push heroku master

and your new app changes will be compiled and deployed. Bewdy.

Good luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment