Heroku is a cloud platform that supports multiple programming languages and frameworks, including Node.js. Deploying an Express.js application to Heroku can be a straightforward process once you have your application ready for production. This tutorial will guide you through the steps required to deploy your Express.js application to Heroku.
Deploying an application to Heroku involves several key steps:
Procfile, and ensuring all dependencies are listed in your package.json.Before deploying, ensure your Express.js application is ready for production. This includes setting up environment variables and configuring a Procfile.
Create a .env file in the root of your project to store environment variables:
1.env2PORT=30003DB_HOST=localhost4DB_USER=root5DB_PASS=s1mpl3
You can use packages like dotenv to load these variables into your application:
1// app.js2require('dotenv').config();34const express = require('express');5const app = express();6const port = process.env.PORT || 3000;78app.get('/', (req, res) => {9res.send('Hello World!');10});1112app.listen(port, () => {13console.log(`App listening at http://localhost:${port}`);14});
Heroku uses the Procfile to determine how to run your application. Create a file named Procfile in the root of your project:
1web: node app.js
If you don't have a Heroku account, sign up at Heroku's website.
Download and install the Heroku CLI from Heroku's official site. Once installed, log in to your Heroku account using the following command:
$ heroku loginEnter your Heroku credentials.Email: user@example.comPassword (typing will be hidden):Authentication successful.
Ensure your application is in a Git repository. If it's not, initialize one with the following commands:
$ git initInitialized empty Git repository in /path/to/your/app/.git/$ git add .$ git commit -m "Initial commit"[master (root-commit) abc1234] Initial commit5 files changed, 100 insertions(+)create mode 100644 .envcreate mode 100644 Procfilecreate mode 100644 app.jscreate mode 100644 package.jsoncreate mode 100644 package-lock.json
Create a new Heroku app and push your code to it:
$ heroku create my-express-appCreating ⬢ my-express-app... donehttps://my-express-app.herokuapp.com/ | https://git.heroku.com/my-express-app.git$ git push heroku masterCounting objects: 10, done.Delta compression using up to 4 threads.Compressing objects: 100% (8/8), done.Writing objects: 100% (10/10), 2.35 KiB | 2.35 MiB/s, done.Total 10 (delta 0), reused 0 (delta 0)remote: Compressing source files... done.remote: Building source:remote:remote: -----> Node.js app detectedremote:remote: -----> Creating runtime environment...remote:remote: NPM_CONFIG_LOGLEVEL=errorremote: NODE_ENV=productionremote: NODE_MODULES_CACHE=trueremote: NODE_VERBOSE=falseremote:remote: -----> Installing binariesremote: engines.node (package.json): unspecifiedremote: engines.npm (package.json): unspecifiedremote: Resolving node version (latest stable) via semver.io...remote: Downloading and installing node 16.13.0...remote: Using default npm version: 8.1.4remote:remote: -----> Installing dependenciesremote: Installing node modules (package.json)remote:remote: added 50 packages, and audited 51 packages in 2sremote:remote: 3 packages are looking for fundingremote: run `npm fund` for detailsremote:remote: found 0 vulnerabilitiesremote:remote: -----> Buildremote: Preparing package.json dependencies for cachingremote: Done caching npm dependenciesremote:remote: -----> Caching buildremote: Clearing previous node cacheremote: Saving 251.7M cache layerremote:remote: -----> Launching...remote: Released v3remote: https://my-express-app.herokuapp.com/ deployed to Herokuremote:remote: Verifying deploy... done.To https://git.heroku.com/my-express-app.git* [new branch] master -> master
Once the deployment is complete, you can open your application in a browser:
$ heroku openOpening https://my-express-app.herokuapp.com/ in your default browser...
You can view logs for your Heroku app using the following command:
$ heroku logs --tail2023-10-05T14:28:37.123Z app[web.1]: Express server listening on port 30002023-10-05T14:29:00.456Z app[web.1]: GET / 200 1ms - 12b
After deploying your Express.js application to Heroku, you might want to monitor its performance and health. You can use Heroku's built-in monitoring tools or integrate third-party services like New Relic or Datadog for more detailed insights.
Heroku also provides features like dynos scaling, which allows your application to handle varying loads by automatically adjusting the number of running instances.