How to Deploy React Express, Node App to Heroku - 2020
UPDATE:
*************************** WATCH SIMPLER VERSION FIRST ***************************
https://youtu.be/xwovj4-eM3Y
***************************************************************************************
*************************** USE FETCH NOT AXIOS ***************************
Also, very important. Make sure you are making your backend calls using fetch not axios.
I found a good answer as to why here:
https://github.com/axios/axios/issues/1358#issuecomment-364789526
***************************************************************************************
If the above two did not resolve the issue go ahead with this video even though this is not my best work :)
ORIGINAL:
Struggling to deploy your react express app to heroku? Trust me, you are not alone. It is a pain to do so and that is why I created this video.
# Repo of the code used in this example
https://github.com/mujibsardar/react_express_heroku_example
# Make sure heroku CLI is installed
https://devcenter.heroku.com/articles/heroku-cli
# Run npm build in your CLIENT/React folder
cd client && npm build
# Add heroku post build script
# Add the following line to package.json on the server/node side:
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build
# Serve client react instead of backend
# Add the follwing code to your server file on the backend
const path = require('path');
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
# Do the same for the following code. Your main server file that is.
// ** MIDDLEWARE ** //
const whitelist = ['http://localhost:3000', 'http://localhost:8080', 'https://shrouded-journey-38552.herokuapp.com']
const corsOptions = {
origin: function (origin, callback) {
console.log("** Origin of request " + origin)
if (whitelist.indexOf(origin) !== -1 || !origin) {
console.log("Origin acceptable")
callback(null, true)
} else {
console.log("Origin rejected")
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors(corsOptions))
# Set heroku nodejs buildpack
# Run the following command
heroku buildpacks:set heroku/nodejs
Please let me know what kind of problems you run into. I would love to help if I can.
Other Helpful Links:
Tutoring: https://avantutor.com
Coding Q&A: https://askavan.com
Coding Bootcamp: https://avansclass.com
Видео How to Deploy React Express, Node App to Heroku - 2020 канала Avan Tutor
*************************** WATCH SIMPLER VERSION FIRST ***************************
https://youtu.be/xwovj4-eM3Y
***************************************************************************************
*************************** USE FETCH NOT AXIOS ***************************
Also, very important. Make sure you are making your backend calls using fetch not axios.
I found a good answer as to why here:
https://github.com/axios/axios/issues/1358#issuecomment-364789526
***************************************************************************************
If the above two did not resolve the issue go ahead with this video even though this is not my best work :)
ORIGINAL:
Struggling to deploy your react express app to heroku? Trust me, you are not alone. It is a pain to do so and that is why I created this video.
# Repo of the code used in this example
https://github.com/mujibsardar/react_express_heroku_example
# Make sure heroku CLI is installed
https://devcenter.heroku.com/articles/heroku-cli
# Run npm build in your CLIENT/React folder
cd client && npm build
# Add heroku post build script
# Add the following line to package.json on the server/node side:
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build
# Serve client react instead of backend
# Add the follwing code to your server file on the backend
const path = require('path');
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
# Do the same for the following code. Your main server file that is.
// ** MIDDLEWARE ** //
const whitelist = ['http://localhost:3000', 'http://localhost:8080', 'https://shrouded-journey-38552.herokuapp.com']
const corsOptions = {
origin: function (origin, callback) {
console.log("** Origin of request " + origin)
if (whitelist.indexOf(origin) !== -1 || !origin) {
console.log("Origin acceptable")
callback(null, true)
} else {
console.log("Origin rejected")
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors(corsOptions))
# Set heroku nodejs buildpack
# Run the following command
heroku buildpacks:set heroku/nodejs
Please let me know what kind of problems you run into. I would love to help if I can.
Other Helpful Links:
Tutoring: https://avantutor.com
Coding Q&A: https://askavan.com
Coding Bootcamp: https://avansclass.com
Видео How to Deploy React Express, Node App to Heroku - 2020 канала Avan Tutor
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
The Road to Learn React - Addon - 16 - Side Effects - 2021Understand React useRef() - Short & SimpleThe Road to Learn React - Addon - 31 - Third Part Library - 2021Learn more about the 100% Free Coding Bootcamp (FreeCodingBootcamp.Org)The Road to Learn React - Addon - 21 - Imperative React - 2021The Road to Learn React - Addon - 20 - Component Composition - 2021The Road to Learn React - Addon - 2 - Meet React Component - 2021The Road to Learn React - Addon - 34 - Class Components - 2021AskAvan Coding Question #122 - Detect Pattern and Then Program It!The Road to Learn React - Addon - 22 - Inline Handler - 2021The Road to Learn React - Addon - 6 - Component Instantiation - 2021Advice #1 - Don't Make This Coding Mistake!The Road to Learn React - Addon - 10 - React Props - 2021The Road to Learn React - Addon - 7 - React DOM - 2021The Road to Learn React - Addon - 8 - React Component Definition - 2021The Road to Learn React - Addon - 26 - Impossible State - 2021Delete Github Repository - 1 Minute - NEW 2021The Road to Learn React - Addon - 9 - Handler Function - 2021Solve (Codewars) JavaScript ChallengesThe Road to Learn React - Addon - 19 - Reusable Components - 2021Free Coding Bootcamp Preview - Watch a Recoding of a Class Session