Deploying to Heroku
From Localhost to the Internet
Running your API on localhost:5000 only works on your own machine. Deployment means copying your code to a remote server so that anyone on the internet can reach it. Heroku is a cloud platform that removes the complexity of managing servers — you push your code and it handles the rest.
What Is Heroku?
Heroku is a PaaS (Platform as a Service) provider. You do not rent a raw server and configure it yourself. Instead, Heroku provides a managed environment called a dyno — a container that runs your application. You push your code, Heroku builds and runs it.
Heroku is popular for learning and small projects because:
- Free tier available for testing (with sleep-on-inactivity)
- Git-based deployment is simple
- Supports Node.js, Python, Ruby, Go, and more
- Add-ons for databases, logging, and monitoring
Setting Up
1. Install the Heroku CLI
Download from https://devcenter.heroku.com/articles/heroku-cli and log in:
heroku login
2. Create a Heroku App
In your project directory (which must be a git repository):
heroku create my-api-name
Heroku adds a remote named heroku to your git config. You can confirm with:
git remote -v
The Procfile
Heroku needs to know how to start your application. Create a file named Procfile (no extension) in your project root:
web: node server.js
The web process type tells Heroku this is an HTTP-serving dyno. If your entry file is index.js or app.js, adjust accordingly.
The PORT Variable — Critical
Heroku assigns a dynamic port to your dyno via the PORT environment variable. Your app must listen on that port, not a hard-coded one:
// WRONG -- will not work on Heroku
app.listen(5000, () => console.log('Server running'));
// CORRECT
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
This is one of the most common deployment errors for beginners.
Deploying
# Make sure all changes are committed
git add .
git commit -m "Prepare for Heroku deployment"
# Push to Heroku
git push heroku main
Heroku will:
- Detect Node.js (via
package.json) - Run
npm install - Run the command in your
Procfile
Once done, open your app:
heroku open
Setting Config Variables (Environment Variables)
Never hard-code secrets in your code. Set them as Heroku config vars:
heroku config:set MONGODB_URI=mongodb+srv://user:pass@cluster.mongodb.net/mydb
heroku config:set JWT_SECRET=mysecretkey123
heroku config:set NODE_ENV=production
View all config vars:
heroku config
These are available in your app as process.env.MONGODB_URI, etc.
Viewing Logs
heroku logs --tail
This streams live logs from your running dyno. Indispensable for debugging deployment issues.
Common Deployment Issues
| Problem | Likely Cause | Fix |
|---|---|---|
| App crashes immediately | Hard-coded port or missing Procfile | Use process.env.PORT, add Procfile |
| Cannot connect to database | MONGODB_URI not set | heroku config:set MONGODB_URI=... |
| H10 error (App crashed) | Unhandled error on startup | Run heroku logs --tail to see the error |
| 503 errors | App is sleeping (free tier) | Access the URL to wake it, or upgrade |
| npm install fails | Missing dependency or wrong Node version | Check package.json engines field |
Set the Node.js version in package.json to match your local version:
{
"engines": {
"node": "18.x"
}
}
Practice Exercise
- Create a minimal Express API with a
GET /route that returns{ message: "Deployed to Heroku!" }. - Add a
Procfileand ensure your app listens onprocess.env.PORT. - Run
heroku createand deploy withgit push heroku main. - Set a
SECRET_KEYconfig var and log it (masked) on startup. - Trigger an error and use
heroku logs --tailto identify and fix it.
Try it yourself
Key Takeaways
- Heroku is a PaaS (Platform as a Service) that lets you deploy Node.js apps without managing servers — just push with git.
- The Procfile tells Heroku how to start your app: 'web: node server.js'.
- Always listen on process.env.PORT — Heroku assigns a dynamic port and your app will receive no traffic otherwise.
- Set secrets and configuration with 'heroku config:set' — never commit .env files or hard-code credentials.
- Use 'heroku logs --tail' immediately after deployment to catch startup errors.
Quick Quiz
1.What is the purpose of a Procfile in a Heroku project?
2.Why must your Express app listen on process.env.PORT instead of a fixed port like 5000?
3.What command streams live logs from a running Heroku dyno?
4.How should you pass secrets like your MongoDB connection string to a Heroku app?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx