# Deploying a Node.js App to Heroku: A Step-by-Step Guide

## Introduction

[Heroku](https://www.heroku.com/) is a cloud platform that enables [developers](https://bytescrum.com/) to deploy, manage, and scale applications. It supports several programming languages, including Node.js, making it a popular choice for hosting Node.js applications. In this guide, we will walk you through the <mark>process of deploying a Node.js app to Heroku</mark>.

### **Step 1: Prerequisites**

Before you begin, make sure you have the following prerequisites:

1. Node.js and npm installed on your local machine.
    
2. A Heroku account. Sign up at [Heroku](https://www.heroku.com/).
    
3. The Heroku CLI installed. You can download it from the Heroku CLI page.
    

### **Step 2: Create a Node.js App**

If you don't already have a [Node.js](https://medium.com/@adnanrahic/hello-world-app-with-node-js-and-express-c1eb7cfa8a30) app, you can create a simple one for this guide. Create a new directory for your app and initialize a new Node.js project:

```bash
mkdir my-node-app
cd my-node-app
npm init -y
```

Create an `index.js` file with a simple HTTP server:

```javascript
const http = require('http');

const PORT = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
```

### **Step 3: Prepare the App for Deployment**

Create a `Procfile` in the root of your project. This file tells Heroku how to run your app:

```bash
echo "web: node index.js" > Procfile
```

### **Step 4: Initialize Git Repository**

If you haven't already initialized a [Git](https://blog.bytescrum.com/how-to-use-git-and-github-for-version-control) repository for your project, do so now:

```bash
git init
git add .
git commit -m "Initial commit"
```

### **Step 5: Login to Heroku CLI**

Login to the [Heroku](https://devcenter.heroku.com/articles/heroku-cli) CLI using the following command:

```bash
heroku login
```

### **Step 6: Create a Heroku App**

[Create](https://devcenter.heroku.com/articles/creating-apps#:~:text=To%20create%20a%20new%20app,CLI%20and%20run%20this%20command.&text=The%20command's%20output%20shows%20that,the%20remote%20Git%20repository%20URL.) a new Heroku app using the Heroku CLI:

```bash
heroku create
```

This will create a new Heroku app and add a new Git remote called `heroku`.

### **Step 7: Deploy the App to Heroku**

[Deploy](https://www.youtube.com/watch?v=DQk3zJlY-eE) your app to Heroku using Git:

```bash
git push heroku master
```

### **Step 8: Open the App**

Open your deployed app in your browser:

```bash
heroku open
```

### **Step 9: View Logs**

View the logs of your app to check for any errors:

```bash
heroku logs --tail
```

### **Step 10: Scale the App**

You can scale your app by adjusting the number of dynos (containers) running your app:

```bash
heroku ps:scale web=1
```

Congratulations! You have successfully deployed a Node.js app to Heroku. You can now continue to develop your app and deploy updates using the same process.

> This guide provides a step-by-step process for deploying a Node.js application to Heroku, covering everything from setting up your environment and creating a simple Node.js app, to deploying and managing the app on Heroku's cloud platform.
