Heroku + Node.js + Sequelize + PostgreSQL – How to Deploy Node.js Web Application with PostgreSQL on Heroku platform using Heroku CLI

deploy-nodejs-with-mysql-on-heroku---feature-image

Heroku is a platform as a service (PaaS) that help us to build, run, and operate applications entirely in the cloud. So in the tutorial, we show you how to deploy Node.js WEB application with PostgreSQL in Heroku.

Related posts:
Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL
Heroku + SpringBoot Deployment example – How to Deploy SpringBoot + PostgreSQL on Heroku platform with Heroku CLI

Technologies

– Heroku
– PostgreSQL
– Node.js
– Sequelize
– Git

Goal

We deploy a Node.js Web Application with PostgreSQL to Heroku platform:

– Heroku PostgreSQL ->

deploy-nodejs-with-mysql-on-heroku---postgresql-db

deploy-nodejs-with-mysql-on-heroku---select-all-rows-in-postgresql-heroku

– Node.js RestAPI ->

deploy-nodejs-with-mysql-on-heroku---deploy-successfully-on-heroku-api-request

Practice

Setup Heroku CLI
Download & Install Heroku CLI

For detail go to the Download Heroku CLI

– Install Heroku CLI on Linux:

-> Details:

Login to Heroku

Use heroku login cmd to login:

deploy-nodejs-with-mysql-on-heroku---Heroku-Login-successfully

deploy-nodejs-with-mysql-on-heroku---Heroku-Login

Add Heroku SSH keys

– To check your key was added, run heroku keys
– If your key isn’t there, you can add it manually by running heroku keys:add

deploy-nodejs-with-mysql-on-heroku---Add-SSH-Keys

Create Simple Node.js Web Application
Create simple Node.js Web Application

Create package.json file:

-> Process command:

-> It creates a package.json file with below content:

– Install Express & Body-Parser:

npm install express body-parser --save

Implement simple Node.js Web App

Inside root folder, create a file server.js file:

Configuration to Run on Heroku
Specify the version of Node.js

To run your application on Heroku, specify the version of Node.js:

Create Procfile

Heroku use a Procfile to start your app. Inside the root folder, create a Procfile with below content:

– If no Procfile exists for a Node.js app, we will attempt to start a default web process via the start script in your package.json:

– The command in a web process type must bind to the port number. -> We create a .env file, inside a root directory.
Then define the application port in it:

Run App Locally

Use below cmd ->

deploy-nodejs-with-mysql-on-heroku---run-locally

Deployment on Heroku
Create a Git Repository

Go to Git Account, create a Git Repository:

deploy-nodejs-with-mysql-on-heroku---create-git-repository

– Keep build artifacts out of git by creating a .gitignore file that looks something like this:

– Add all of the code to Git by running these commands:

Create a new Heroku App

– Use the below commandline to create Heroku App:
heroku create

deploy-nodejs-with-mysql-on-heroku---create-heroku-app

-> It creates a new Heroku App with random name: shrouded-island-81150

deploy-nodejs-with-mysql-on-heroku---create-heroku-app-running

– We can rename the app using the cmd ->

$ heroku apps:rename --app oldname newname

-> Details:

deploy-nodejs-with-mysql-on-heroku---update-name-app

deploy-nodejs-with-mysql-on-heroku---nodejs-app-rename

Deployment

– Now deploy your code by commandline:

git push heroku master

-> Logs:

-> Visit the app’s URL by running this command: heroku open

Result:

deploy-nodejs-with-mysql-on-heroku---helloworld

Deploy Node.js Web Application with PostgreSQL on Heroku
Heroku Attach PostgreSQL Database

– Attach a PostgreSQL database to your application by cmd:

heroku addons:create heroku-postgresql

deploy-nodejs-with-mysql-on-heroku---create-postgresql-database

– List the configuration variables by cmd: heroku config & heroku pg

Update Node.js App accessing PostgreSQL with Sequelize

– Install Sequelize:

deploy-nodejs-with-mysql-on-heroku---install-sequelize-postgresl

-> Now package.json ->

– Setting up Sequelize MySQL connection ->

Create ./app/config/env.js file:

Setup Sequelize-MySQL connection in ./app/config/db.config.js file:

– Create Sequelize model ./app/model/customer.model.js ->

– Define Customer’s routes in ./app/route/customer.route.js file ->

– Implement Customer’s controller in ./app/controller/customer.controller.js file ->

– Update server.js ->

Re-deployment Nodejs on Heroku

– Add the change to Git Repository ->

-> Deploy code:

git push heroku master ->

Results

– Heroku PostgreSQL ->

deploy-nodejs-with-mysql-on-heroku---postgresql-db

deploy-nodejs-with-mysql-on-heroku---select-all-rows-in-postgresql-heroku

– RestAPIs requests ->

deploy-nodejs-with-mysql-on-heroku---deploy-successfully-on-heroku-api-request

– Logs $heroku logs --tail ->

SourceCode

Node.js-Express-PostgreSQL-Application

By grokonez | December 19, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*