Aashir Digital

aashirdigital.

How to make Progressive Web Apps in Reactjs Nodejs

1) Open index.html file

<script>
      if ("serviceWorker" in navigator) {
        window.addEventListener("load", () => {
          navigator.serviceWorker
            .register("./serviceWorker.js")
            .then((res) => {
              console.warn("Worker Registered");
            })
            .catch((err) => {
              console.warn("Error in sw", err);
            });
        });
      } else {
        console.error("Service workers are not supported in this browser.");
      }
    </script>

2) Create a new file in public folder named as serviceWorker.js

Add Below Code in serviceWorker.js file

//STORAGE OF BROWSER
const CACHE_NAME = "version-1";
const urlsToCache = ["index.html", "offline.html"];
const self = this;

//installation
self.addEventListener("install", (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => {
      console.log("Opened cache");

      return cache.addAll(urlsToCache);
    })
  );
});

// listen for request
self.addEventListener("fetch", (event) => {
  event.respondWith(
    caches.match(event.request).then((res) => {
      return fetch(event.request).catch(() => caches.match("offline.html"));
    })
  );
});

// actitivate the service worker
self.addEventListener("activate", (event) => {
  const cacheWhitelist = [];
  cacheWhitelist.push(CACHE_NAME);
  event.waitUntil(
    caches.keys().then((cacheNames) =>
      Promise.all(
        cacheNames.map((cacheName) => {
          if (!cacheWhitelist.includes(cacheName)) {
            return caches.delete(cacheName);
          }
        })
      )
    )
  );
});

3) Open manifest.json file

Add below code in manifest.json file

{
  "name": "Your App Name",
  "short_name": "Your App Name",
  "theme_color": "#066759", //
  "background_color": "#f7f7f7",
  "display": "standalone",
  "scope": "/",
  "start_url": "/",
  "description": "a short description for your app",
  "icons": [
    {
      "src": "mask.png",
      "sizes": "512x512",
      "type": "image/png",
      "purpose": "any maskable"
    },
    { "src": "logo192.png", "sizes": "192x192", "type": "image/png" },
    { "src": "logo256.png", "sizes": "256x256", "type": "image/png" },
    { "src": "logo384.png", "sizes": "384x384", "type": "image/png" },
    { "src": "logo512.png", "sizes": "512x512", "type": "image/png" }
  ]
}

4) Now you have to create logo of different sizes

192×192 | 256×256 | 384×384 | 512×512

Go to website – https://imageresizer.com/

Copy all the logos and paste it into public folder as shown in the video

5) Now you have to create a Maskable icon

Go to website – https://progressier.com/maskable-icons-editor

Upload your logo and create a maskable icon then copy and paste it into public folder

6) Hurray! you have completed all the step for your Progressive Web App

You will see an install option at the right corner and in mobile view users can see a popup to install the progressive web app.

How to upload MERN project on AWS EC2

1. Create Free AWS Account

Create free AWS Account at https://aws.amazon.com/

2. Create and Launch an EC2 instance and SSH into machine

I would be creating a t2.medium ubuntu machine for this demo.

3. Install Node and NPM

Option 1 — Installing Node.js with Apt from the Default Repositories

sudo apt update

Option 2 — Installing Node Using the Node Version Manager

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

source ~/.bashrc

nvm list-remote

Output
. . .
        v18.0.0
        v18.1.0
        v18.2.0
        v18.3.0
        v18.4.0
        v18.5.0
        v18.6.0
        v18.7.0
        v18.8.0
....

nvm install v18.17.0

nvm list

Output
->     v14.10.0
       v14.21.2
default -> v14.10.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.21.2) (default)
stable -> 14.21 (-> v14.21.2) (default)
. . .

4. Clone your project from Github

git clone https://github.com/your-repo-name/project-name

5. Install dependencies and test app

sudo npm i pm2 -g
pm2 start index

# Other pm2 commands
pm2 show app
pm2 status
pm2 restart app
pm2 stop app
pm2 logs (Show log stream)
pm2 flush (Clear logs)

# To make sure app starts when reboot
pm2 startup ubuntu

6. Install NGINX and configure

sudo apt install nginx

sudo nano /etc/nginx/sites-available/default

Add the following to the location part of the server block

server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://localhost:8001; #whatever port your app runs on
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
# Check NGINX config
sudo nginx -t

# Restart NGINX
sudo nginx -s reload

8. Add SSL with LetsEncrypt

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

# Only valid for 90 days, test the renewal process with
certbot renew --dry-run

How to Send SMS using Nodejs and Fast2sms India

Step 1: Sign up for a Fast2SMS Account

  • Go to the Fast2SMS website (https://www.fast2sms.com/) and sign up for an account.
  • Once you’re logged in, go to your dashboard and navigate to the API section to get your API key.

Step 2: Set Up Your Node.js Project

  • Create a new directory for your project and open a terminal or command prompt in that directory.

mkdir fast2sms-integration || cd fast2sms-integration || npm init -y

Step 3: Install the axios package

  • axios is a popular HTTP client for making requests. You’ll use it to send requests to the Fast2SMS API.

npm install axios

Step 4: Write the Node.js Code

  • Create a file named sendSMS.js and add the following code:
  • Make sure to replace 'YOUR_API_KEY' with your actual Fast2SMS API key and 'SENDER_ID' with the sender ID you want to use.

Step 5: Run the Code

  • Open your terminal or command prompt and run the following command to execute the script:

node sendSMS.js

This will send a test SMS using Fast2SMS to the specified phone number.