Deploy a Node Website with Express and Heroku

Node.js is a server-side JavaScript environment built on Google Chrome’s engine. It allows you to write JS on the server for quick front and back-end development. In this tutorial, we're going to install Node and use NPM (Node Package Manager) to include dependencies for our project. Once our website is up an running, we will deploy it to Heroku, a cloud application platform.

Download Node and NPM: https://nodejs.org/en/

For Linux users,

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Package.json

Create a package.json file that includes our project dependencies.

{
    "name": "myApp",
        "version": "0.0.1",
        "private": "true",
    "dependencies": {
        "express": "4.13.3",
        "jade": "*",
        "ejs": "2.3.4"
    }
}

Express is the web framework. You can declare other tools such as jade and/or ejs templating engines. Server.js

  • Requires Express as our main framework to build the Node application.
  • The localhost port is set to 5000 for testing locally.
  • The static file public folder location is where our main web files such as index.html, css and image files will live.
/*
 * Module dependencies
 */
var express = require('express'); 
var app = express(); 
app.set('port', (process.env.PORT || 5000));
 
// set location for the static files 
app.use(express.static(__dirname + '/public')); 
// Set default template engine to "ejs"
app.engine('html', require('ejs').renderFile); 
// Views directory for all template files
app.set('views', __dirname + '/views'); 
app.set('view engine', 'html'); 
app.get('/', function (req, res) { 
    res.render('index');
});

app.listen(app.get('port'), function () { 
    console.log('Node app is running on port',
    app.get('port'));
});

The server will render the index file as our home page. To test the Node application, open the command prompt, navigate to the project folder root and type in:

node server.js

You should get the message:

Node app is running on port 5000

Test in a browser by navigating to localhost:5000

Now that we have a website running on the local machine, we’ll use Heroku to send it live. Heroku is a cloud platform service that supports several programming languages. Once deployed, the website URL will look like this: `generated-app-name.herokuapp.com`

Heroku

Go to Heroku Dev Center: https://devcenter.heroku.com/

  • Setup a free account
  • Download Heroku Toolbelt (Package for using the command line)

Open Git Bash and clone the working project to your local GitHub folder:

git clone https://github.com/url.git

Navigate to the project’s root in GitHub:

cd node-app

Create a new file called, Procfile, which tells Heroku what command should be executed to start the app. Include the following:

web: node server.js

Prepare Heroku to receive the source code by entering:

heroku create

Heroku will generate a random name for the app that can be changed later.

Deploy the code:

git push heroku master

Open the website by entering:

heroku open

Or by navigating to the app’s url, generated-app-name.herokuapp.com.

Our site is now live on the Heroku platform and we can easily edit and monitor changes using Git.