Deploying a Node.js server-side backend CRUD REST service to Heroku connected to a cloud-based MongoDB Atlas database

Setting Up MongoDB Atlas On The Cloud

Figure 1 : Database Deployments screen at cloud.mongodb.com (MongoDB Atlas)
Figure 2 : Connect to Cluster() in cloud.mondodb.com (MongoDB Atlas)
Figure 3 : Choose a connection method to connect to our database cluster
Figure 4 : Paste connection string to connect to the database
Figure 5 : …or connect using connection parameters
Figure 6 : Add IP Access List Entry before you can connect to DB
Figure 7 : Network Access lists all the authorised IP able to access the DB
Figure 8 : Databases list in cloud.mongodb.com (MongoDB Atlas)
Figure 9 : Adding sample data to our DB

Modifying Our Local Node.js CRUD Backend for Heroku

//Connect to Mongo DB — localmongoose.connect(‘mongodb://localhost:27017/myapp’, {useNewUrlParser: true}, () => {console.log(‘Connected to MongoDB…’);});
//Connect to Mongo DB — now with cloud accessmongoose.connect(process.env.MONGODB_URI || ‘mongodb://localhost:27017/myapp’ || ‘mongodb+srv://<account_name>:<your_password>@cluster0.95wjk.mongodb.net/test’, {useNewUrlParser: true}, () => {console.log(‘Connected to MongoDB…’);});
app.listen(3000);
app.listen(process.env.PORT || 3000);
Figure 9.1 : Procfile required in project for Heroku
web: node app.js

Deploying to Heroku with Heroku CLI

Figure 10 : Your Heroku Dashboard
Figure 11 : Heroku Deploy screen
Figure 12 : Downloading Heroku CLI
Figure 13 : Checking Heroku version from Visual Code editor
> heroku --version
heroku/7.0.0 (darwin-x64) node-v8.0.0
> heroku login
heroku: Press any key to open up the browser to login or q to exit
› Warning: If browser does not open, visit
› https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
> heroku create <your-project-name-in-Heroku>

Using Git To Deploy our node.js App to Heroku

> git add . 
> git commit
> git push heroku master
Figure 14: Test your newly-deployed node.js app — display users list (/users)

--

--

--

38+ years as a programmer. Started with VB, now doing Java, SpringBoot, Android, React, ASP and Python. Running my own software company at mysoftware2u.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

fork() — what is it?

WagyuSwap Tasty Facts

Adventures in Command Writing

Introducing — Image Sitemap for Shopify

Everything you need to know about Carbon within Laravel

#APIs in the Travel Industry

5 Mental Shifts That Helped Me Build a 6-Figure Online Business in My 20s

Chaos engineering: An introduction

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ezani

Ezani

38+ years as a programmer. Started with VB, now doing Java, SpringBoot, Android, React, ASP and Python. Running my own software company at mysoftware2u.com

More from Medium

Node.js Basic Concept

Fetch emails and file attachments using NodeJS

JS : The Illusion of multi-threading

Short, Unique, & Unguessable: Generating Short Links in Twirl