Snakcability BE
Recommend Software
Editor: VS Code MYSQL Workbench MAMP Deployment-> Heroku Postman

The code uses ES6 syntax, which requires a transpiler to convert into ES5 syntax. In order for this to run without errors on the heroku server (production mode), which is Linux based, you need to build the app with babel cli commands. I've already written the scripts for this.

If you're simply running the code locally, in development mode, just make sure you have a MySQL server running that is connected to a local MySQL database, then make sure your environment variables in .env are correct (the DB information you used to set up the MySQL server should match what's in the .env), and then cd into the backend repository root and run 'npm run start'. If all is well, it should start a server for the application on localhost.


Steps to deploy a production release:

FRONTEND ==============================================================================

git checkout ProductionExpo
Make sure that the various SEARCH_URL strings in all the functions query http://snackability-backend.herokuapp.com INSTEAD OF backendURL (which is exported from environment.js, and only pertains to development/local).
Make sure that searchNdbno() has restrictions passed in the SEARCH_URL, or Dietary Restrictions will not work.
run 'yarn start' from the command line (inside the frontend repo root) to start the application
A browser window will open and once the tunnel is ready, on the left hand side click the link that says Publish or Republish project...
It should publish the frontend successfully, which is already tied to the Heroku backend




BACKEND ===============================================================================

Merge any changes from your development branches that are needed into master branch

Switch to master branch after merging, and check the following:

Open ./server/config/config.js and ./server/config/dbConfig.js

Make sure that dbConfig has the following:

setInterval(function(){ db.query('SELECT 1'); }, 5000);

This keeps the heroku server alive, which tends to randomly stop and start Also, make sure that the environment variables are being imported from config.js

Inside config.js, make sure the exported variables all point to process.env.

If you don't have a .env file (this is your first time in the repo), create a .env file at the backend repo root and copy paste the following:

For Development: NODE_ENV=development DEVELOPER=<YOUR_NAME> DB_HOST= DB_USER= DB_PASSWORD= DB_DATABASE=snackability_2

The .env file sets these variables, which are environment specific. This means when you're in development mode, just open the .env file and make sure it has the right values for each variable.

For Production: Production is different, you must set the variables on the heroku server directly, since .env files are gitignored and cannot be added to the heroku server.

From the command line, cd into snackability-backend

Run 'heroku login'

Enter the snackability-dev credentials (see Palacios for the details)

Now you should be logged into the heroku CLI

Run 'heroku config:set <ENVIRONMENT_VAR_NAME>= For example, if you want to set DB_HOST to foo, you would run:

heroku config:set DB_HOST=foo

You must do this for all the env variables in .env! The config for production is below:

DB_HOST=us-cdbr-iron-east-01.cleardb.net DB_USER=b8ee767b54cdf6 DB_PASSWORD=e495fc4d DB_DATABASE=heroku_8c771ba488c5da8 CRON_SEC=0 CRON_MIN=0 CRON_HR=14

Once you have the environment variables set on the heroku server, verify them by running 'heroku config'

If all the environment variables are set correctly, all you need to do is open package.json (on master branch) and make sure that it has the following:

"scripts": { "start": "npm run build && node ./dist/app.js", "clean": "rm -rf dist && mkdir dist", "build-babel": "babel -d ./dist ./server -s --source-maps inline --copy-files", "build": "npm run clean && npm run build-babel" }, "heroku-run-build-script": true

Explanation of each command: "build" --> Because of the "heroku-run-build-script": true line, heroku will run build first when building the app, which simply runs "clean" and then runs "build-babel" "clean" --> rm -rf dist will delete the entire dist directory, and mkdir dist will create an empty directory called dist again "build-babel" --> babel -d ./dist ./server -s is essentially telling babel that the DESTINATION directory is ./dist, and the SOURCE directory is ./server. Babel will take what is in the source directory, transpile it to ES5 so it can run on the Linux server, and then place the converted code into the destination directory, ./dist. The --source-maps inline --copy-files flag is needed because we have .json files that need to be copied for the feedback strings, and by default .json files are not copied with the babel cli command. "start" --> This runs after the build is successful, and essentially re runs everything, then calls node app.js on the app.js present in the ./dist folder, which is the transpiled one that the heroku server understands.

The package.json scripts for dev and production are plaintext files that are part of the repo. They should be adjacent in the hierarchy to this README.