November 24, 2023
I deployed my first NodeJS application

TL;DR
I deployed a basic NodeJS app on a VM and connected it to a domain name.
Backstory
I've been trying to build web applications for a few months. This itself has been a rewarding challenge to take on, one which I'm still on. I know nothing outside of how to submit a form and get its data to the server, and send client the response.
Apart from this, I've constantly wondered about how the common websites one visits, become 'visit-able', along with how can one website be visited by multiple people at the same time, be available at all times.
I eventually got to learn about Vercel and how to easily deploy web applications via it. Simply hook up your GitHub repository and assuming its a NextJS application (I've thus far only deployed NextJS apps so I'm not sure how to deploy vanilla JS apps or other frameworks'), it will simply deploy it.
But I continued itching at how can a local application be deployed for the public to visit it.
How I did it
I came across this amazing article which was for most part a great guide expect with a few outdated commands or libraries.
I bought the cheapest VM (Ubuntu 23, 1 GB RAM, 25 GB; $6/month), updated it after opening the console, downloaded Git and Node. I cloned the repository in which the web app was in, and I fired up the NodeJS server. I could finally see the website when visiting the URL comprised of VM's IP and the port on which server ran. It freaking worked!
- More on how I learned why the pages for routes wouldn't render despite having created them. Spoiler alert: I had POST API endpoints but not GET API endpoints for each route, and I also learned that I had to send the respective HTML file for each route, in the GET route.
I had a domain name lying around so I edited its DNS records to include the VM's IP address, type as A, and host as 'www'. I then successfully visited www.gocreate.ca:3000, after some time.
I then learned about how a reverse proxy can receive a request with a general version of the URL and send it to server at a more specific URL. Example of this is typing in www.gocreate.ca but actually be served www.gocreate.ca:3000. This was done via Nginx
I also learned to secure the reverse proxy. The domain name is added to Nginx's config file, beside server_name in server block. It is then checked for errors, and then Nginx process is reloaded. Certbot is installed (minor difference from the guiding article: something about the PPA repository no longer existing so I used Certbot's doc), and then process to generate SSL certificate for the proxy using Certbot is executed. After this, https://www.gocreate.ca works.
Next, Nginx configurations for the web app are made, in the same config file. I'm currently very uncertain about why these changes are made and what they each mean: proxy_pass is changed, ssl_certificate_key is changed, and another server block with code is added. Config file is checked for errors and reloading of the proxy is done.
My question of how my application will stay online, is now explored. Once I close the console to the VM, it shuts off the processes in which the node server is running and therefore, website becomes unavailable. Concept of process manager comes to light, PM2 is installed, and ran on boot. Start up script is created. Web app is then started using PM2, and saved to start again on boot.