20/03/2021 - deploy node app to heroku

this is a react app run on express server app. backend and frontend.


tidying up the directories and pre production code
server js
// const __dirname = path.resolve();
if ((process.env.NODE_ENV = 'production')) {
  app.use(express.static(path.join(__dirname, 'my-app/build')));
  app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, 'my-app', 'build', 'index.html'));
  });
}
package json
 "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix "<frontend folder>" && npm run build --prefix "<frontend folder>"
procfile
web: node src/server.js
.gitignore
/nodemodules
.env


//test run your app before deploy
.env 
NODE_ENV=production
//terminal
cd <frontend folder>
npm run build
cd ..
cd <backend folder>
npm run start
//change back to development
//deploying to heroku
1. push to git repo
2. install heroku cli
3. run heroku command to create app and push code
4. add config vars
5. trouble shoot
//terminal
git init
git add .
git commit -m '<comment>'
git add remote origin <url>
git branch -m <main branch name, use "main">
git push origin main
//install heroku cli on windows if you havent
//terminal
heroku login
heroku create <app name>
heroku git:remote -a <app name>
heroku push main
..
//heroku>your app>settings>reveal config vars>{keys, PORT, NODE_ENV}>open app
..
heroku open
..
heroku logs --tail 
//heroku does not read env files you have to add n environment variables yourself
//heroku push>config vars>open herou app>open log> refresh app> check log>troubleshoot>push to git and heroku again

//if you have an errors you need to fix and deploy your code again to git and run heroku push main


references


Comments

Popular posts from this blog

08/06/2020 - scoped css with sass + react

11/02/2021 - git commands for remote repo

29/11/2021 - time flies