# Node JS Setup

Setting up Node.js for a project like **WaSnap** involves several steps to ensure a smooth development process. Here's a breakdown of the setup process:

the steps for setting up Node.js hosting account, fulfilling server requirements, deploying the project source files, updating npm, and installing and using PM2 for server management:

## #Prerequisites

Before running the project, make sure you have the following installed:

* [Node.js](https://nodejs.org/)

## #Getting Started

* **Set up Node.js Hosting Account**: Choose a hosting provider that supports Node.js hosting. Sign up for an account and follow their instructions to set up a server instance.
* **Fulfill Server Requirements**:
  * Ensure that the Node.js version on your server meets the project's requirements (Node version 20+).
  * Install npm and npx for dependency management. These are typically included when you install Node.js. If not, you can install them manually using the Node.js package installer.
* **Deploy Project Source Files**:
  * Upload all project source files to a main directory on your server. You can use tools like FTP or SSH for file transfer.
* **Update server dependencies:**&#x20;

<pre><code>``` bash
<strong>    apt update &#x26;&#x26; apt upgrade -y
</strong>```
</code></pre>

* **Download the code to your local machine from codecanyon.net and Deploy to your server:**

````bash
  ``` bash
      scp -r /Path/To/Your/App root@your_vps_ip:Path/To/Remote/Dir
  ```
````

* **Install dependencies :**

````
``` bash
    npm install
```
````

* **Update npm**:
  * Connect to your sel0rver via SSH and navigate to the directory where your project is located.
  * Run the command `npm update` to update npm and download any required dependencies and packages for your project.
* **Install PM2**:
  * Install PM2 globally on your server using npm:
  * ```
    npm install pm2 -g
    ```
* **PM2 Commands**: PM2 is a Production Process Manager for Node.js applications with a built-in Load Balancer.

After installing PM2, you can use various commands to manage your Node.js processes:

* Start a Node.js application:
* <pre><code><strong>$ pm2 start wasnap-bot.js  (Use this command to start server)
  </strong></code></pre>

<figure><img src="https://2663140616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfdnmCtivPgIGEf1E3BVl%2Fuploads%2FWtnuD29zj28hLtPAZIml%2Fimage.png?alt=media&#x26;token=33beeb9e-6848-459e-b2e6-6fe3a787c690" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2663140616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfdnmCtivPgIGEf1E3BVl%2Fuploads%2F5QDJ5PtCATvcKjYXfsn1%2Fimage.png?alt=media&#x26;token=94433c8f-607e-41eb-8281-0fdbd974542f" alt=""><figcaption></figcaption></figure>

* Load balance multiple instances of a Node.js application:
* ```
  $ pm2 start wasnap-bot.js  -i 4
  ```
* Monitor PM2 processes:
* ```
  $ pm2 monitor
  ```
* Configure PM2 to auto-boot at server restart:
* ```
  $ pm2 startup
  ```

For more information and advanced usage, refer to the PM2 documentation: [PM2 Documentation](https://pm2.keymetrics.io/)

**The server will be running at :** <mark style="color:blue;"><http://your\\_server\\_url:PORT>.</mark>

{% hint style="success" %}
**You successfully setup Node JS Server** :confetti\_ball:
{% endhint %}

{% hint style="danger" %}
**Node.JS is not required for send message via Twilio.**
{% endhint %}

### **Node.js Server URL Details add in Laravel Project:**

* The server is built using Node.js, with the Express framework to handle HTTP requests and responses.
* Locate the `.env` File: The `.env` file is located in the root directory of your Laravel project. Use a text editor to open the file.
* Find APP\_DOMAIN\_NAME: Look for the section in the `.env` file that contains the APP\_DOMAIN\_NAME configuration variables and add your Node server URL.

<figure><img src="https://2663140616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfdnmCtivPgIGEf1E3BVl%2Fuploads%2FTLnzkY5OPak9UFSBndlh%2Fimage.png?alt=media&#x26;token=54ba5351-4d3d-401e-a651-d5678cf82b7f" alt=""><figcaption></figcaption></figure>
