Deploy nodejs on Heroku
To deploy nodejs on Heroku, first ensure you have Node.js installed and the Heroku Command Line Interface (CLI). You’ll also need a Heroku account.
Prepare your application by configuring its package.json
file to include a “start” script, which Heroku uses to run your app, for example, "start": "node src/app.js"
. Your application should also listen on the port specified by the PORT environment variable, which Heroku provides (e.g., process.env.PORT || 3000
). It’s advisable to install development dependencies locally using --save-dev
.
Use Git for version control. After initialising your Git repository and committing your code, create a new Heroku application by running heroku create
in your project’s root directory. This command also sets up a Heroku Git remote for your project. To deploy your code, simply push your changes to this Heroku remote: git push heroku master
.
Node.js Application Deployment: Getting Your App Live
When a Node.js application is deployed, it becomes available to users online. Node.js applications can be developed locally, but in a live environment, accessibility, continuous operation, and effective in management must be taken into account. A popular web server, Node.js serves up JSON APIs, webpages, and more.
Heroku Integration: Deploying to a Cloud Platform
A well-liked cloud platform called Heroku provides a simplified method for launching Node.js apps. By managing a large portion of the underlying infrastructure, it offers a managed environment that frees developers to concentrate on their application code.
This is the standard method for deploying to Heroku:
- Install the Heroku CLI (Command Line Interface): With this tool, you may use your terminal to interface with Heroku. Following installation, you must locally log into your Heroku account:
- By requesting your email address and password, this command will safely link your local computer to your Heroku account.
- Prepare Your Application for Heroku: Heroku Application Preparation Heroku requires a few small changes to your Node.js application.
- Port Configuration: Using an environment variable named
PORT
, Heroku creates a dynamic port for your application. This port should be open for your application to listen on.Process.env.PORT
can be set to a common port, such as3000
, if it is not configured (for example, during local development). - start Script in package.json: Heroku must understand how to launch your program. Your
package.json
file’sscripts
section has the details for this. Astart
script that runs your primary server file should be defined:- This enables your application to be booted by Heroku using
npm start
.
- This enables your application to be booted by Heroku using
- Port Configuration: Using an environment variable named
- Create and Deploy
- Start by making a new Heroku application in the root directory of your project:
- With this command, your app will have a unique URL, and your local repository will have a new Git remote called
heroku
. - After that, send your code to the Heroku remote:
- Heroku builds and deploys your application, installs dependencies, and detects the push.
- Lastly, launch the deployed application in your browser:
- This command opens the application at its own Heroku URL.
- Heroku also lets you set up production environment variables, such API keys or database credentials, so they don’t get into your code. You can use
heroku config:set KEY=VALUE
to set these.
Version Control with Git/GitHub
Git is an essential version control technology that lets programmers monitor changes to their codebase over time. For managing deployment routines, rolling back to earlier versions, and collaborative development, this is essential.
Git repositories are hosted by the well-known web-based platform GitHub, which also makes code backup, project management, and collaboration easier.
Important Git ideas and deployment commands consist of:
- Initialising a Git Repository: Start using Git to track your project:
- Staging Changes: Choose the files that will be part of your upcoming commit:
- This adds to the staging area all of the modified files in the current directory.
- Committing Changes: The staged modifications should be committed by adding a descriptive message to your local Git history.
- As a result, a snapshot of your project at that moment is created.
- Pushing to a Remote Repository: Uploading your local commits to a distant server, such as GitHub or Heroku, is known as “push to a remote repository.” Once a remote has been configured (for example,
git remote add origin <GitHub_URL>
), you push your modifications: - By doing this, you back up your code to GitHub and make it accessible to others.
To remove created files and directories from version control, such the node_modules
folder, which is easily regenerated using npm install
, it is standard practice to utilize a .gitignore
file. Your repository stays clean and gets smaller as a result.
SSH keys are frequently used for safe communication with distant repositories. You can create a pair of SSH keys and add the public key to your Heroku or GitHub account.
Deployment Strategies of Process Managers Running as a Service
Node.js applications must function constantly and be fail-proof in a production setting. The Node.js application itself is a single-threaded process. Node.js applications are operated as a service or daemon using process managers to benefit from multi-core systems and provide high availability.
These instruments aid in:
- Keep the application alive forever: If the app crashes, restart it automatically.
- Manage processes: Keep an eye on how it being used and streamline management.
- Enable zero-downtime deployments: Reloading the application with updated code without disrupting service is one way to enable zero-downtime deployments.
In the Node.js ecosystem, PM2 and Forever are two well-liked process managers:
- PM2 (Process Manager 2): For Node.js applications, PM2 (Process Manager 2) is a production-ready process manager.
- Installation:
- Starting an application:
- Additionally, PM2 may launch apps in cluster mode to take use of several CPU cores and efficiently divide the workload:
- When the
-i 0
flag is set, PM2 is instructed to launch as many processes as there are CPU cores. - Zero-Downtime Reloads: The
pm2 reload
feature gracefully restarts without any downtime for updates. It sends aSIGINT
signal to processes to gracefully end, waits for them to disconnect, and then initiates fresh processes. - Useful Commands:
- List all running processes:
pm2 list
- Stop an app:
pm2 stop my-app
- Restart an app:
pm2 restart my-app
- View logs:
pm2 logs my-app
- List all running processes:
- Forever: A straightforward command-line utility that guarantees a script executes perpetually.
- Installation:
- Starting an application:
- Constantly tracks your process and restarts it in case it crashes.
- Useful Commands:
- Restart application:
forever restart 0
(where0
is the process ID) - Stop application:
forever stop 0
- Restart application:
To ensure that Node.js apps run as daemons on Linux-based computers and are controlled by the operating system, systemd
is another popular init system.
Although Nodemon is helpful for development because it restarts the server automatically when code changes, it is generally not advised for usage as the primary process manager in production. PM2 and Forever are built to withstand the demanding demands of real-world settings.