Deploying an Express App to Heroku
Some coworkers expressed interest in deploying Express apps to Heroku. These instructions seek to provide a basic overview, though Heroku offers much more robust documentation in its dev center.
Step 1: Create a Heroku account.
Step 2: Install the Heroku Toolbelt, which includes the Heroku command line client, Git, and Foreman.
Step 3: Log in by entering the following in the command line:
heroku login
Step 4: Install Node.js.
Step 5: Create an Express app.
Let’s call it heroku-demo
:
mkdir heroku-demo
Define the application dependencies via a heroku-demo/package.json file:
{
"name": "heroku-demo",
"description": "Basic Express.js/Heroku demo",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x"
}
}
Install the package:
npm install
Set up the app via a heroku-demo/app.js
file:
var express = require("express");
var app = express();
// Set up a URL route
app.get("/", function(req, res) {
res.send("Heroku Demo!");
});
// bind the app to listen for connections on a specified port
var port = process.env.PORT || 3000;
app.listen(port);
// Render some console log output
console.log("Listening on port " + port);
Now, you can run your app locally and view it in your browser at http://localhost:3000
:
node app.js
Step 6: Declare your app’s process types with a heroku-demo/Procfile
so that it can run with Foreman:
web: node app.js
This declares a “web” process, as well as the command needed to run it. You can test that your Procfile/Foreman works:
foreman start
Step 7: Make heroku-demo
a Git repository:
git init
git add .
git commit -m "Initial commit"
Step 8: Deploy heroku-demo to Heroku.
Create a Heroku app:
heroku create
Deploy the code to Heroku:
git push heroku master
Step 9: View your Heroku-hosted app in your web browser:
heroku open
Some Additional Heroku CLI Tips
Make your app available at a custom newname.herokuapp.com subdomain:
heroku apps:rename newname
You can also use custom domains. See the Heroku documentation.
View environment variables:
heroku config
More Heroku environment variable documentation.
Add environment variables:
heroku config:add NODE_ENV=production
View logs:
heroku logs
View running processes:
heroku ps
Learn more about the Heroku command line client:
heroku help