Deploy to Heroku

Prereqs:

Heroku setup

Application setup

You need to make a few changes to get your application hosted on Heroku: setting up a static express server to serve your application, tell Angular where you want your application built, and tell Heroku how to build your application.

Setting up your express server

In the terminal in your application directory, run npm install --save express. This will add express to serve your static Angular application.

Next in VSCode, you want to add a file called server.js in the root of your project (next to your package.json file), and paste in the folling content:

// server.js
const express = require('express');
const app = express();
const path = require('path');

const forceSSL = function() {
  return function (req, res, next) {
    if (req.headers['x-forwarded-proto'] !== 'https') {
      return res.redirect(
       ['https://', req.get('Host'), req.url].join('')
      );
    }
    next();
  }
}
// Instruct the app
// to use the forceSSL
// middleware
app.use(forceSSL());

// Run the app by serving the static files
// in the dist directory
app.use(express.static('dist'));

app.get('/*', function(req, res) {
  res.sendFile(path.join('dist/index.html'));
});

// Start the app by listening on the default
// Heroku port
app.listen(process.env.PORT || 8080);

This will set up a server which will use https to serve your application from where Angular has built your application.

Change the npm start command

Now we need to make a distinction between how we want to run the application on your computer (in development mode) vs on Heroku (in production mode).

Update your package.json file to have the following changes to your scripts section:

  "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "local": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "heroku-postbuild": "npm run build"
  },

This is doing a few things:

Setup your Heroku engines

In your package.json file, you will need to add a section to tell Heroku what engines to use. Add this section under your "scripts" section.

"engines": {
    "node": "<node version>",
    "npm": "<npm version>"
},

In your terminal in your project directory run the following things to fill in the info above:

Changing Angular’s build location

The last thing we need to change is where you application is being built to so the express server can get to it. In your angular.json file, search for outputPath and change that from dist/<your project name> to just dist.

DEPLOY!

Now that you have made all these changes, you should have the following changed files:

And one new file

Commit and push to GitHub, and head back to Heroku. On the overview tab, you should see that you have a running build. Once that is done, click on Open app to view your application. This will take a few seconds for you application to wake up, but give it time. You should now see your application running.

Happy Coding!

Home