Angular4 deploy to Heroku: MySportsFeeds API Part 2

Deploy an Angular4 app to Heroku.

  • For Heroku this app uses a node.js server app.js with express.
  • All routes will be going to dist/index.html.
  • Run ng build to build the app in the dist directory.
  • Run node app.js to serve the app at http://localhost:3001.
  • The Procfile in this app’s root specifies the server for heroku to use.
//Procfile

web: node app.js

  • This "main": "app.js" line in package.json specifies how to tell heroku to look for app.js.
  • Before pushing to github, before heroku deploy set Config Variables.

Adding the environment variable for the MySportsFeeds api. I didn’t want to share my api headers information in my github repository so I added my password to my Config Variables for heroku to use in the app settings from the Heroku dashboard. I stored the password in my heroku app by going to the app settings in my heroku dashboard. Click on Config Variables and add the key (name) and value (password) there. It will be secured safely away from human view. You can call it to the client side by adding this code to the app.js file. I called my env TOKEN and made the value MySportsFeeds password.

  • Use the Heroku Client API to retrieve the TOKEN from the app and then send it to the front-end of the angular app like this.
  • I used my heroku account token to authenticate Heroku Client. I saved it to the config vars of this app as API_TOKEN.
//app.js 

const express = require('express');
const http = require('http');
const path = require('path');
const Heroku = require('heroku-client')
const heroku = new Heroku({ token: process.env.API_TOKEN })
const api = require('./server/routes/api');
const app = express();

let TOKEN = '';

app.use(express.static(path.join(__dirname, 'dist')));

//GET CONFIG VAR FROM SPECIFIC HEROKU APP
heroku.request({
  method: 'GET',
  path: 'https://api.heroku.com/apps/my-app-name/config-vars',
  headers: {
    "Accept": "application/vnd.heroku+json; version=3",
    "Authorization": "Bearer "+process.env.API_TOKEN
  },
  parseJSON: true
}).then(response => {
  //console.log(response.API_KEY, "heroku api from server");
  TOKEN = response.TOKEN;
})

//SEND CONFIG VAR TO FRONT-END APP.COMPONENT.TS
app.get('/heroku-env', function(req, res){
        res.write(TOKEN);
        res.end();
});

//SPECIFY NG-BUILD PATH
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'))
});

//HEROKU PORT
const port = process.env.PORT || '3001';
app.set('port', port);

const server = http.createServer(app);
server.listen(port, () => console.log(`Running on localhost:${port}`));

After you git push to your repo follow the steps below. Assuming you have a heroku account and installed the heroku toolbelt.

    1. run heroku log in
    2. run heroku create name-of-app
    3. run git push heroku master
    4. If deploy is successful run heroku open

If there were problems during deploy and you are trying this from scratch here are some requirements heroku needs to deploy.

    1. Have @angular/cli and @angular/compiler-cli listend under dependencies in package.json.
    2. have a server/routes directory with api.js
//api.js

const express = require('express');
const router = express.Router();

/* GET api listing. */
router.get('/', (req, res) => {
  res.send('api works');
});

module.exports = router;

References for deploying Angular4 to heroku: https://medium.com/@ervib/deploy-angular-4-app-with-express-to-heroku-6113146915ca

Check the codebase on my GitHub account!