The following will need to be known / obtained before getting started:
- Node.js and npm
- command-line (cli) / terminal
- Heroku account
- Heroku CLI installed and setup with your Heroku account
- In the root directory for
tab-tracker, create a newpackage.jsonfile. The contents of the file should be similar to this:
tab-tracker/package.json
{
"name": "tab-tracker",
"version": "1.0.0",
"description": "",
"scripts": {
"heroku-postbuild": "npm run build-client && npm run install-server",
"start": "node server/src/app.js",
"build-client": "cd ./client && npm install && npm run build",
"install-server": "cd ./server && npm install"
},
"engines": {
"node": ">= 4.0.0"
},
"cacheDirectories": ["client/node_modules", "server/node_modules"]
}name can renamed to your app's name, you can add a description if you'd like, but the scripts, engines, and
cacheDirectories sections should be identical.
-
Open the
client/directory'spackage.jsonand move all of thedevDependenciesto thedependenciessection. This is best done by copying and pasting inside your text editor.client/'spackage.jsonshould not have adevDependenciessection now. -
Open your cli or terminal of choice with Heroku CLI installed and navigate to the root directory of your app (i.e.,
tab-tracker/). Run the following command:
$ heroku create tab-tracker # replace tab-tracker with your app name- Next, change the
baseURLin thesrc/services/Api.jsfile found in theclient/directory to the URL of your new Heroku app.
tab-tracker/client/src/services/Api.js
import axios from 'axios'
import store from '@/store/store'
export default () => {
return axios.create({
baseURL: `https://tab-tracker-demo.herokuapp.com/`, // <-- url copied from `heroku create ...` command
headers: {
Authorization: `Bearer ${store.state.token}`
}
})
}- Edit the
server/'ssrc/app.jsfile to includepathand serve the builtclient/resources. Addconst path = require('path')to the top with the other constants and addapp.use(express.static(path.join(__dirname, '../../client/dist')))after the otherapp.use(...)declarations.
tab-tracker/server/src/app.js
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const {sequelize} = require('./models')
const config = require('./config/config')
const path = require('path') // <-- added
const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(cors())
app.use(express.static(path.join(__dirname, '../../client/dist'))) // <-- added
require('./passport')
require('./routes')(app)
sequelize.sync({force: false})
.then(() => {
app.listen(config.port)
console.log(`Server started on port ${config.port}`)
})- Add a Heroku remote using the following command:
$ heroku git:remote -a tab-tracker # again, replace tab-tracker with your app's nameThis sets up a git remote with Heroku so you can deploy your app via git, as seen in the next steps.
- Save and commit the changes using
git add .andgit commit -m '[MESSAGE]', for example:
$ git commit -m 'Readying for Heroku deployment'- Assuming you are on the
masterbranch, run the following command to push your code to Heroku, which initiates the deploy process.
$ git push heroku masterIf you're not on the master branch, you can use the following command:
$ git push heroku localbranch:master # where localbranch is the non-master branch- Wait for
git pushprocess to finish then you're all set! 🎉
Assuming the deployment went smoothly and nothing failed during the build process, your app should now be deployed
and ready to be view at https://<your_app_name>.herokuapp.com/. The demo app used for testing this guide can be found here:
https://tab-tracker-demo.herokuapp.com/
Note: Heroku uses a ephemeral filesystem which means when your dyno goes down due to inactivity, all file changes made
during it's uptime get erased. This means any changes to the database (i.e., new user registrations, songs added, etc.)
will be gone. Sequelize can interact with PostgreSQL databases so adding a Heroku Postgres database to your app and configuring
the server/src/config/config.js file to point to your Heroku Postgres database will allow for data persistance. Outlining that
process will be reserved for another time and guide however...