Deploying NestJS Application: Easy and Explained
Tags: node, webdev, beginners, javascript
Published: 2020-09-16 08:19:06

### Nest: Quick Overview
<hr>
> The biggest rule of programming is--*DRY (Don't Repeat Yourself)*. 

<hr>

* If one wants to write a more efficient code whilst developing any kind of application, they must practice DRY and [SOLID](https://medium.com/@mari_azevedo/s-o-l-i-d-principles-what-are-they-and-why-projects-should-use-them-50b85e4aa8b6) principles.

* These principles are the base of the famous *Model-View-Controller (MVC)* design pattern. The Angular Framework for building Frontend is entirely based on this design pattern. It uses concepts of components and services to implement DRY and SOLID Principles. 

* Not only the increase in performance, understanding Angular code is quite easy due to this. 
<hr>
* There was no backend framework that makes the task easy in the same way as Angular does in frontend. That's where Nest originated.

* Nest is a backend framework that is heavily inspired by Angular.
![Angular and Nest](https://i.morioh.com/b7793ed298.png)
* It uses the concepts of services, controllers, and modules to implement MVC pattern in a way similar to Angular.
<hr>
> I highly recommend using Nest when working on larger projects rather than using Express.

<hr>
**The focus of this article is on the deployment of Nest Applications, if you want me to post beginner or advanced level tutorials on learning Nest.js, comment below:-)**
<hr>

### How to Deploy NestJS Application?: The Theory
When we want to deploy Angular App, what we basically do is we compile the application to JavaScript and then host it on any static application hosting provider like GitHub, AWS S3, etc.

We host Nest Application in a similar way. We compile the Nest Application to JavaScript and host it on Dynamic Application hosting providers like Heroku, A2Hosting, AWS EC2, AWS Elastic Beanstalk, etc.

<hr>
### How to Deploy NestJS Application?: The Platform Generic Steps

* First, open `.gitignore` file and remove the following line to enable pushing the compiled javascript files to the remote repository so that we can download them on the server for hosting.

<center>Remove `/dist` from `.gitignore`</center>

* In `package.json` make sure that it contains scripts defined as
```json
"build": "nest build",
 "start": "nest start",
"start:prod": "node dist/main",
```

* Some Hosting Providers use Procfile for running web applications, so create a file in the project folder named `Procfile` and add the following single line to it.
```procfile
web:npm run start:prod
```
* Delete the `dist` folder and run `npm run start` in command-line/terminal to create the latest version of compiled output files. (Deleting folder first is not necessary but it has proven helpful in some cases).
* Push your changes to GitHub Repository using these three basic commands:
```bash
git add . //Stage All Files including dist folder
git commit -m "Commit Message"
git push
```
* Setup your cloud instance on any platform:
  * [AWS EC2 Instance](https://www.guru99.com/creating-amazon-ec2-instance.html)
  * [A2Hosting](https://www.a2hosting.in/kb/cpanel/cpanel-software/create-application-with-nodejs-selector)
* Clone the GitHub repository to the instance using the terminal.
* Target your server to launch `dist/main.js` if required to specify.

In this way, your NestJS app will be deployed. 
<hr>
> This process is attested and followed by developers of www.era-co.in

<hr>
Thanks for Reading!
*What should I post about next? Do tell me in the comments. Suggestions, improvements, and additions are invited.*