Backend Documentation

This documentation provides a comprehensive guide to the backend architecture and functionality of the Strapi application at DevLaunchers. It aims to ensure smooth collaboration and faster onboarding.

What is Strapi?

Strapi is a headless CMS (Content Management System) designed for flexibility and ease of use. As a headless CMS, Strapi allows developers to build and manage content while delivering it through APIs to various platforms, such as websites, mobile apps, and IoT devices.

Key benefits of using Strapi:

  • API-Centric: Strapi automatically generates REST or GraphQL APIs for all content types, making it easy to integrate with any frontend.

  • Customizable: Developers can extend or customize Strapi to suit specific project needs by modifying its controllers, services, or plugins.

  • Ease of Management: Content editors can use Strapi's intuitive admin panel to manage and organize content efficiently without developer intervention.

At DevLaunchers, Strapi plays a crucial role in managing content and serving APIs for various applications, ensuring a seamless flow of data across environments.

Technical Overview

Strapi is built on the following core technologies:

  • Framework: Node.js - a JavaScript runtime for building scalable backend applications.

  • Language: JavaScript - the primary language used for development within the Strapi ecosystem.

  • Database:

    • Development Environment: SQLite - a lightweight relational database used locally for ease of setup during development.

    • Production and Test Environments: PostgreSQL - a robust relational database ideal for handling large-scale production data and testing scenarios.

Project Overview

This section provides an overview of the project structure for the backend powered by Strapi. It is essential to understand the purpose and functionality of each major folder and file. This section will break down the structure into key components for better clarity.

Key Folders and Their Purposes

  1. src/ This is the core directory for the backend logic and Strapi implementation. It contains all the custom configurations, APIs, components, and other Strapi-related features.

    • api/: This folder includes all the APIs that power the backend. Each sub-folder corresponds to a specific content type or feature. For example:

      • applicant/, project/, notification/, and others represent individual APIs created within Strapi.

      • Each API folder includes the following subfolders and files:

        • controllers/: Handles the logic for the specific API endpoints.

        • services/: Contains reusable business logic.

        • models/: Defines the data structure for the API.

        • routes/: Configures the available API routes for the content type.

    • components/: Houses reusable data structures or logic that can be shared across multiple APIs. For example:

      • people/, positions/, and links/ may define reusable building blocks or relationships used in content types or APIs.

    • extensions/: Contains custom extensions or plugins added to Strapi.

    • middlewares/: Holds custom middleware for handling requests and responses.

  2. config/ This folder contains the configuration files for various environments (e.g., development, production). It includes the database configurations, server settings, and other environment-specific files.

    • database/ Manages database configurations and related setups. It ensures the backend can connect to and interact with the correct database based on the environment.

  3. public/ Stores publicly accessible files such as uploaded assets, images, or static resources.

  4. .env A key configuration file for managing sensitive environment variables such as database credentials, API keys, and Strapi configurations.

  5. Dockerfile and Dockerfile.dev These files contain instructions for containerizing the application using Docker. This is helpful for ensuring consistent development and deployment environments.

Environment SetUp

Local Development Using Node.js

To set up the Strapi backend for local development with Node.js, follow these steps:

  1. Node.js Version Requirement: Ensure that Node.js v20 is installed. Using the correct version is crucial for compatibility with the Strapi application.

  2. Set Up a Version Manager: It is recommended to use a version manager like NVM (Node Version Manager) or NPM Version Manager (n) to manage Node.js versions easily.

  3. Install Dependencies and Initialize the Database: Run the following command from the project’s root directory to install all required dependencies and initialize the local SQLite database for development:

    npm run init
  4. Start the Development Server: Launch the local development server using:

    npm run develop
  5. Access the Strapi Admin Panel: Once the server is running, access the Strapi Admin interface by navigating to http://localhost:1337/admin in your browser. Use the following credentials to log in:

    • Email: local-admin@devlaunchers.org

    • Password: W&x5ZzOMtBCVt1YAUiuSzt~5

Using Docker

For developers who prefer a containerized setup, the backend can be run using Docker. Follow these steps:

  1. Ensure Docker is Running: Make sure Docker Desktop is active or that the Docker daemon is running on your system.

  2. Build the Docker Container: Navigate to the project’s root directory and build the Docker container by running:

    make build-docker
    • This command builds the containerized environment for the Strapi backend. The process may take a few minutes, especially if it’s the first time.

  3. Start the Strapi Server: After building the container, start the Strapi server using:

    make run-docker
    • The server will automatically reload when you make changes to the project files, eliminating the need to restart the container manually.

  4. Restart the Server if the Container Stops: If the Docker container goes down or needs to be restarted, run:

    make run-docker
    • Note: Rebuilding the container (using make build-docker) is only necessary if significant changes are made to the configuration.

API Usage

Authentication

Strapi APIs are secured by default. To interact with protected routes, users or developers need to authenticate using JSON Web Tokens (JWT). Authentication tokens are generated upon successful login and are required to access most API endpoints.

  • Public Endpoints: Can be accessed without authentication (if configured).

  • Protected Endpoints: Require valid JWT tokens.

Login Endpoint: Use this endpoint to authenticate and obtain a JWT:

POST /api/auth/local
Content-Type: application/json
Body: 
{
  "identifier": "user@example.com",
  "password": "password123"
}

Response:

{
  "jwt": "eyJhbGciOiJIUzI1NiIsInR5...",
  "user": { ... }
}

Using REST APIs

Strapi's REST APIs allow developers to interact with content types using standard HTTP methods:

  • GET: Retrieve content.

  • POST: Create new entries.

  • PUT: Update existing entries.

  • DELETE: Remove entries.

Example REST API Endpoints:

  1. Get All Entries for a Collection:

    GET /api/{collection-name}

    Example:

    GET /api/articles
  2. Get a Single Entry by ID:

    GET /api/{collection-name}/{id}

    Example:

    GET /api/articles/1
  3. Create a New Entry:

    POST /api/{collection-name}
    Content-Type: application/json
    Body: 
    {
      "title": "New Article",
      "content": "This is an example."
    }
  4. Update an Existing Entry:

    PUT /api/{collection-name}/{id}
    Content-Type: application/json
    Body: 
    {
      "title": "Updated Title"
    }
  5. Delete an Entry:

    DELETE /api/{collection-name}/{id}

Last updated