Node JS

Description
A lightweight platform ideal for building fast, scalable network applications in Javascript. Similar in design to Ruby's Event Machine or Python's Twisted, and built on Chrome's JavaScript runtime, NodeJS is ideal for data-intensive apps that run across distributed devices.
Software Included
Package | Version | License |
---|---|---|
Node.js | 12.18.1 | Custom |
NPM | 6.14.4 | Artistic License 2.0 |
NGINX | 1.17.10 | Custom |
PM2 | 4.4.0 | GNU AGPL v3 |
Getting started after deploying NodeJS
After you created a Server, you can navigate to its public IPv4 address to see the sample application live.
You will then be prompted to enter a password. If you created your VPS with a root user password, enter this in the terminal. If you created your Droplet with an ssh key, enter the passphrase associated with your key.
Using MongoDB Database
To connect to the database with your NodeJS 1-Click App, you will need the CA Certificate which is also pre-configured on your Droplet at boot time. You can find the certificate at /root/dbaas_ca_cert.crt.
Here is sample Node.js app showing a connection over TLS using a CA certificate:
require('dotenv').config() const MongoClient = require('mongodb').MongoClient; // Connection string which you can copy from your database dashboard under "connection details". // Don't forget to replace the password and tlsCAFile parts of the string with the appropriate details. const url = process.env.DATABASE_URL; // Default database name. const dbName = 'admin'; // Create a new MongoClient, passing in the location of your CA certificate. const client = new MongoClient(url, { tlsCAFile: `${__dirname}/ca-certificate.crt`, }); // Use the connect method to connect to the server. client.connect(err => { if (err != null) { console.log("failed: ", err); return; } console.log("Connected successfully to mongodb server"); // Example of connection to the default database. const db = client.db(dbName); client.close(); });
To learn how to make modifications or get your code onto your Server, here are the steps. You can also follow the sample application for the instructions.
Step 1: Access to your Server
Open a terminal on your computer to access your Droplet as the root user using the following command:
ssh root@your_droplet_public_ipv4
Step 2: Make modifications to the sample application
Modify this script at /var/www/html/hello.js. You can then see the results live by using pm2, a process manager that schedules your code to run at boot time. The application runs as the nodejs user, so changes to pm2 need to be run as the nodejs user.
sudo -u nodejs pm2 restart hello
Step 3: Get your code onto the Server
Clone your NodeJS code onto the droplet, anywhere you like. Note: If you're not using source code control, you can directly upload the files to your droplet using SFTP.
cd into the directory where your NodeJS code lives, and install any dependencies. (For example, if you have a package.json file, run npm install.)
Launch your app
sudo -u nodejs pm2 start <your-file>
Map the port your app run on to an HTTP URL
nano /etc/nginx/sites-available/default
Edit the existing entry that exposes the "hello" app at port 3000 to the world so that it points to your app's port instead.
Enable your new nginx config.
sudo systemctl restart nginx
Call to schedule your code to run at launch.
sudo -u nodejs pm2 save
Repeat these steps for any other NodeJS apps that need to run concurrently -- schedule them to run at boot time on whatever internal port you like using PM2, then map that port to an HTTP/HTTPS URL in the nginx config. Build out the URL directory structure you need by mapping applications to URL paths; that's the reverse proxy method in a nutshell!
Now you can delete the sample app we installed on your VPS. Stop running the initial script (that you're viewing now)
sudo -u nodejs pm2 delete hello
Stop it from running on Droplet boot.
sudo -u nodejs pm2 save
Step 4: Get production-ready
There's a lot you'll want to do to make sure you're production-ready. Here are the popular things that people will do.
Non-root user: Set up a non-root user for day-to-day use
Firewall: Review your firewall settings by calling sudo ufw status, and make any changes you need. By default, only SSH/SFTP (port 22), HTTP (port 80), and HTTPS (port 443) are open.
Domain: Register a custom domain
Storage: You can mount a volume (up to 16TB) to this server to expand the filesystem, provision a database cluster (that runs MySQL, Redis, or PostgreSQL), or use a Space, which is an S3-compatible bucket for storing objects.
Recap: Deploying NodeJS Applications with EasyCloudify
With EasyCloudify, setting up and managing your applications becomes seamless. The platform supports NodeJS, a powerful JavaScript runtime that enables developers to build scalable network applications with ease.
To ensure reliability in production, EasyCloudify integrates essential tools such as NGINX for load balancing and routing, and PM2 for efficient process management. This combination allows applications to stay online, restart automatically, and scale as needed.
For data-driven projects, connecting to a MongoDB database is simple and secure. When deployed on a VPS, EasyCloudify provides full flexibility, including reverse proxy configurations that make it easy to map services to clean and secure domains.
In short, EasyCloudify delivers a complete environment for modern development, giving businesses the infrastructure they need to run fast, secure, and scalable applications in the cloud.