Deploying NestJS Application: Easy and Explained

Deploying NestJS Application: Easy and Explained

·

3 min read

Nest: Quick Overview


The biggest rule of programming is--DRY (Don't Repeat Yourself).


  • If one wants to write a more efficient code whilst developing any kind of application, they must practice DRY and SOLID 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.


  • 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

  • It uses the concepts of services, controllers, and modules to implement MVC pattern in a way similar to Angular.

    I highly recommend using Nest when working on larger projects rather than using Express.


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:-)


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.


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.
Remove /dist from .gitignore
  • In package.json make sure that it contains scripts defined as

    "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.

    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:
    git add . //Stage All Files including dist folder
    git commit -m "Commit Message"
    git push
    
  • Setup your cloud instance on any platform:
  • 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.


This process is attested and followed by developers of era-co.in


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