GithubHelp home page GithubHelp logo

heroku-node-deploy-guide's Introduction

Heroku Deployment Guide

Prep - Setup Heroku Account + CLI

In order to deploy Node JS apps to Heroku, we need two things:

Afterwards we are able to deploy NodeJS apps.

Please visit the given two links to setup both.

Find here the general guideline for the creation & deployment of NodeJS APIs:

https://devcenter.heroku.com/articles/deploying-nodejs

Deployment preparation in code

Oursource confidential information

  • Create a file .env (in top level of your project)
  • Outsource all confidential info like secrets + DB connection strings + allowed Frontend URL into .env file
  • Add .env to gitignore to prevent secrets being pushed to our codebase
  • Adapt PORT too (process.env.PORT) => because we cannot dictate the port on provider

A typical sample .env file:

MONGO_URI=mongodb+srv://youruser:[email protected]/your_db?retryWrites=true&w=majority
JWT_SECRET=silencio_i_am_secret
FRONTEND_ORIGIN=http://localhost:3000

Load environment into app

  • Install dotenv package npm i dotenv

  • Load environment variables in server.js with require("dotenv").config()

  • Load environment variables in the seed script too

  • Test your app locally, if it still connects to MongoDB (npm start + npm run seed)

  • Add & Commit all code changes

Setup Heroku app & deployment

  • Login to Heroku: heroku login

  • Create Heroku app: heroku apps:create YOUR_APP_NAME

  • Create environment variables on Heroku (either in app settings online: https://dashboard.heroku.com/apps or in terminal)

    • in terminal: heroku config:set KEY=VALUE
  • Check if environment variables were placed successfully on Heroku server: heroku config

  • Perform Deploy: git push heroku <yourLocalBranchName>:main

  • Optional: Perform seed against Heroku database (in case you use a different one locally & centrally): heroku run "npm run seed"

  • Open Webpage: heroku open

Troubleshooting

In case you receive "Application error" on any route of the deployed webpage

  • View the central errors in the error log heroku logs or just output last error heroku logs --tail

Common issues:

  • forgot to use process.env.PORT as port to listen too?
  • forgot start script in package.json? Should be start: "node server.js"
  • env variables spelling issues? compare heroku config with your keys in .env file - mismatch?

DONE!

heroku-node-deploy-guide's People

Contributors

losrobbos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.