An app I'm creating uses the MERN stack (MySql, Express, React, Node). The frontend and backend end are each stored in 2 separate Githubs repos and are hosted on 2 separate Heroku instances. Both the frontend and backend deploy to Heroku successfully.
The question is how do I get both the frontend and backend to communicate with each other? Should the user start at the frontend heroku which will call the backend or should they start at the backend?
I've tried setting the frontend Proxy address to the heroku link for the backend but apparently this Proxy address is only used in the development environment. I've tried adding a server file to front end which eliminates the "Invalid Host header" error but doesn't solve the communication issue.
Here is the frontend package.json:
{
"name": "healthy-front-end",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"express": "^4.17.1",
"materialize-css": "^1.0.0-rc.2",
"moment": "^2.24.0",
"react": "^16.8.6",
"react-datepicker": "^2.8.0",
"react-dom": "^16.8.6",
"react-modal": "^3.9.1",
"react-moment": "^0.9.2",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"dev": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "npm run build"
},
"proxy": "http://localhost:3001",
"eslintConfig": {
"extends": "react-app"
}
}
Here is the backend server.js:
require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
const env = process.env.NODE_ENV || 'development';
const reactConfig = require(path.join(__dirname, '/config/config.static.json'))[env];
const PORT = process.env.PORT || 3001;
// Define middleware here
app.use(express.static(path.join(__dirname, reactConfig))); // serving react files
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
// Serve up static assets
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
// Routes
require('./controllers/routes')(app);
// Start the API server
app.listen(PORT, () =>
console.log(` ==> API Server now listening on PORT ${PORT}!`)
);
Here is the config.static.json file with the front end url:
{
"development": "client/public",
"production": "https://healthy-people-front-end.herokuapp.com/"
}
Here is the config.js file with prod url added:
let config = {
local: {
mysql:{
url: process.env.DB_URL,
},
apiKeys:{}
},
prod: {
mysql:{
url: process.env.JAWSDB_URL,
},
url: 'https://healthy-people-front-end.herokuapp.com/',
apiKeys:{}
}
};
module.exports = config[process.env.APP_ENV || 'local'];
Here is the backend package.json:
{
"name": "healthy-back-end",
"version": "1.0.0",
"description": "API for HealthMate App",
"main": "server.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"dev": "concurrently \"npm start\" \"npm run client\"",
"client": "cd ../HealthMate-frontend && npm run start"
},
"author": "",
"license": "ISC",
"dependencies": {
"concurrently": "^4.1.2",
"cors": "^2.8.5",
"dotenv": "^8.1.0",
"express": "^4.17.1",
"hashpass": "0.0.0",
"mysql": "^2.17.1",
"nodemon": "^1.19.1",
"uuid": "^3.3.2"
}
}
How do I get both ends to communicate with each other? Any help is greatly appreciated.