Skip to content

Latest commit

 

History

History
121 lines (88 loc) · 3.38 KB

ApiSetup.md

File metadata and controls

121 lines (88 loc) · 3.38 KB

How to Deploy the API

This guide outlines how to deploy and configure the API via the commandline. For this setup, we used DigitalOcean droplets to create a simple VPS for hosting the API.


Deploying the API on a Server

Steps to Set Up the API

Note: This assumes that you already have a working database with the correct structure. For details, see the API Database Setup.

  1. Clone the Repository
    Start by cloning the entire repository onto your server:

    git clone <repository_url>
  2. Install Node.js
    If Node.js is not already installed on your server, you need to install it first. Use the following commands to install Node.js and npm (Node.js package manager) on Ubuntu-based systems:

    sudo apt update
    sudo apt install -y nodejs npm

    You can check if Node.js and npm are installed correctly by running:

    node -v
    npm -v
  3. Install Dependencies
    Navigate to the project directory and install all necessary dependencies using npm:

    cd <project_directory>
    npm install
  4. Create a .env File
    If you previously created a .env file before cloning the project and it was added to .gitignore, you will need to create it again.

    To create the .env file follow the instructions in Create the .env file

  5. Start the API
    Run the following command to start the server:

    node app.js

    The API should now be accessible on the configured IP and port. For a list of endpoints, refer to the API Endpoints documentation.


Configuring the API to Run Continuously

To ensure the API remains active even after you close the console, you can configure it to run as a systemd service.

Steps to Configure a systemd Service

  1. Create a Systemd Service File
    Add a systemd service file at /etc/systemd/system/API.service with the following content:

    [Unit]
    Description=Node.js API Service
    After=network.target
    
    [Service]
    Group=www-data
    WorkingDirectory=/var/www/JIC-P.O.S/API
    ExecStart=/usr/bin/node /var/www/JIC-P.O.S/API/app.js
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

    Note: Update the file paths (/var/www/JIC-P.O.S/API) to match the actual location of your API files. It's recommended to place your application in /var/www (create this directory if it does not exist).

  2. Enable and Start the Service
    Use the following commands to enable and start the service:

    sudo systemctl enable API.service
    sudo systemctl start API.service
  3. Verify the Service
    Check the status of the service to ensure it is running:

    sudo systemctl status API.service

Useful Commands for Managing the API Service

  • Restart the Service
    To restart the API service after making changes:

    sudo systemctl restart API.service
  • Stop the Service
    To stop the API service:

    sudo systemctl stop API.service
  • View Logs
    To view logs generated by the service:

    journalctl -u API.service

With these steps completed, your API should be deployed and running continuously on the server.


Back to README