Node + MongoDB + Mongoose + Dokku

Documentation of how to get a Node app hosted on Dokku with a Mongo database using Mongoose as a driver.

The Stack

  • Node + Express server
  • MongoDB - database tech, hosted online use Atlas.
  • Mongoose - mongoDB “driver,” simplifies some of the features of using mongo.
  • Dokku - Hosting platform web apps.

Prerequisites

Setup Process

Clone Starter Code

GitHub - awdriggs/dokkutest

  • Run npm install

Connect the Database

  • Get the url for your database from Atlas, choose Drivers, then Mongoose
    image.png
screenshot from mongo webpage showing connect button
  • Copy the URI String, yours will look something like this but will have different values for username, password, and app name.
screenshot from mongo webpage showing the mongo uri to a database
  • Create a .env file. Add the url for the database: MONGO_URI=mongodb+srv://awdriggs:<db_password>@dokkutest.n2sq9.mongodb.net/retryWrites=true&w=majority&appName=dokkutest
  • Update the URI with the database name. MONGO_URI=mongodb+srv://awdriggs:<db_password>@dokkutest.n2sq9.mongodb.net/DATABASENAME?retryWrites=true&w=majority&appName=dokkutest
  • This is SUPER IMPORTANT! You must specify a database name in the URL, if you don’t MongoDB automatically creates one called test and will put your collections there. This will work fine when you are testing locally but will break when you host on dokku.
  • Get in the practice of adding your database name to the string. You can use a different one for testing and for when you site is live. It took many hours and a little luck to debug this.

Now, you can run the project locally using nodemon server.js and you should see this message in your node console.

screenshot from terminal showing a sucessful connection between node and mongodb

If you don’t see this message double check your URI.

Test by adding some data, should appear on the homepage and also the MongoDB Atlas interface. Troubleshoot if this isn’t working before proceeding.

Deploy to Dokku

  • Connect to dokku using ssh.
  • Create a new app, dokku apps:create dokkutest
  • Setup domain, dokku domains:set dokkutest dokkutest.awdrokku.site
  • Setup letsencrypt, dokku letsencrypt:enable dokkutest
  • Add the URI to the environment variables for this dokku app, using the URI string that you setup for local development. Change the database name if you want a different db for production. dokku config:set dokkutest MONGO_URI="mongodb+srv://awdriggs:<db_password>@dokkutest.n2sq9.mongodb.net/DATABASENAME?retryWrites=true&w=majority&appName=dokkutest"
  • From local, setup dokku remote git remote add dokku dokku@dokkutest
  • Push to dokku, git push dokku main
  • Watch the deployment logs for errors. If sucessfull you’ll see a message like this.
screenshot from terminal showing a successful deploy
  • If you see an error, scroll through the logs to see if there are any hints to what went wrong.
    • From the dokku host, type command dokku trace:on , try to push again from local. There will be longer logs.
    • From the dokku host, type command dokku logs <app-name> to see any of the console.log messages that you app is using.

Dev Notes

Important! The MONGO_URI must also include the db name

mongodb+srv://USERNAME:PW@undersurveillancedata.6wo4x.mongodb.net/locs?retryWrites=4
Screenshot from mongodb website showing a collection

Without locs in the url string, a connection was able to be made during local dev. By default MongoDB adds a test db. Locally you could read and write to the database. BUT this caused huge problems when you pushed to Dokku. A connection would not be established. Adding the db name fixed this. It took you too many hours to find this.

Resources