- Introduction
- Prerequisites
- Tech stack
- Running locally
- Running using docker compose
- Directory structure
- API documentation
This document provides instructions for setting up and running the backend part of the application. The backend is developed using Strapi, a headless CMS that leverages Node.js for server-side operations. Strapi offers an easily manageable admin panel alongside RESTful or GraphQL API capabilities. For this project, we are specifically using PostgreSQL as our database due to its robustness, scalability, and reliability. The following setup instructions will guide you through configuring Strapi to work seamlessly with PostgreSQL, ensuring a solid foundation for your application's data management needs.
- Node.js installed - Download link.
- PostgreSQL installed (if running locally without Docker) - Download link.
- Docker and Docker Compose installed (for Docker environment) - Download link
Database: PostgreSQL
Container: Docker
To get your Strapi backend up and running locally, follow these steps:
-
Navigate to the backend directory:
- Open a terminal and change your current directory to the backend part of your application using
cd backend
.
- Open a terminal and change your current directory to the backend part of your application using
-
Install dependencies:
- Run
npm install
to install all required dependencies. This command reads thepackage.json
file and installs all the libraries and packages listed under dependencies and devDependencies.
- Run
-
Configure environment variables:
- Create a
.env
file in the root directory of your backend. This file should contain all the necessary environment configurations for your project. Use the.env.example
as a template if your project includes one. For a Strapi project using PostgreSQL, you should at least configure the following variables:DATABASE_HOST
: The hostname of your PostgreSQL server.DATABASE_PORT
: The port on which your PostgreSQL server is running (typically 5432).DATABASE_NAME
: The name of your PostgreSQL database.DATABASE_USERNAME
: Your database user's username.DATABASE_PASSWORD
: The password for your database user.
- Make sure your PostgreSQL database is accessible with these credentials.
- Create a
-
Run strapi in development mode:
- Use
npm run develop
to start your Strapi backend in development mode. This mode activates the auto-reload feature, allowing you to see changes in real-time without manually restarting the server. - Once Strapi is running, you can access the admin panel by navigating to
http://localhost:1337/admin
in your web browser. Here, you'll be prompted to create an admin user on your first visit.
- Use
-
Start the backend in production mode (Optional):
- If you need to simulate a production environment locally, you can start your backend using
npm run start
. This command runs Strapi without the development mode's auto-reload feature, simulating how your application will run in a production environment.
- If you need to simulate a production environment locally, you can start your backend using
- Before running your Strapi backend, ensure that your PostgreSQL database is set up and that the
.env
file correctly points to this database. - In development mode, you might encounter additional prompts to migrate or update your database schema, especially after making changes to your content types through the Strapi admin panel. Follow the on-screen instructions to complete these updates.
By following these steps, you can effectively run and develop your Strapi backend with a PostgreSQL database on your local machine.
Docker Compose facilitates the running of multi-container Docker applications. By using a docker-compose.yaml
file, you can configure your application’s services, networks, and volumes in a single file, then start all services with a single command. For our Strapi backend, which uses PostgreSQL as the database, Docker Compose simplifies the process of starting the backend alongside the database and any other services your application might need.
- Docker and Docker Compose installed on your machine.
- Basic knowledge of Docker concepts.
-
Locate the
docker-compose.yaml
File:- Ensure you are in the project's root directory, where the
docker-compose.yaml
file resides. This file orchestrates the setup of the entire application, including the Strapi backend service, the PostgreSQL database service, and the Next.js frontend service. By defining these services, thedocker-compose.yaml
file facilitates an integrated environment where each component of the application—backend, database, and frontend—can operate cohesively.
- Ensure you are in the project's root directory, where the
-
Review and update the docker compose configuration:
- Open the
docker-compose.yaml
file and review the configurations. Make sure the Strapi service has the correct volume mappings to persist data and is configured to communicate with the PostgreSQL service. The PostgreSQL service should have environment variables (POSTGRES_DB
,POSTGRES_USER
, andPOSTGRES_PASSWORD
) that match those expected by your Strapi application.
- Open the
-
Environment variables:
- If your Strapi application requires specific environment variables, ensure they are defined within the Docker Compose file under the
environment
section of the Strapi service or through an.env
file referenced in the compose file.
- If your Strapi application requires specific environment variables, ensure they are defined within the Docker Compose file under the
-
Starting the services:
- From the terminal, run
docker-compose up
to start all services defined in thedocker-compose.yaml
file. If you prefer to run the services in the background, usedocker-compose up -d
.
- From the terminal, run
-
Accessing the strapi admin panel:
- Once the services are up and running, you can access the Strapi admin panel by navigating to
http://localhost:1337/admin
on your web browser. The first time you access it, you'll be prompted to create an admin user.
- Once the services are up and running, you can access the Strapi admin panel by navigating to
- To stop and remove all the running services, use
docker-compose down
. If you want to preserve the data in your volumes, make sure not to use the-v
flag with this command.
By following these steps, you can smoothly run your Strapi backend with PostgreSQL and any other necessary services using Docker Compose, ensuring a consistent and isolated environment for development and testing.
The backend
folder of a Strapi v4 project contains several important directories and files that structure the application. Here is an overview of some of the key components:
-
api/
: This directory houses the models, services, and controllers for your application's various entities. Each subdirectory withinapi/
corresponds to a specific content type created in Strapi, containing files that define the structure (models), business logic (services), and the API endpoints (controllers). -
config/
: Contains configuration files for various aspects of your Strapi application. This includes database settings, plugin configurations, middleware settings, and more. Key files includedatabase.js
for database configuration,server.js
for server settings, andplugin.js
for plugin configuration. -
extensions/
: Used for extending or overriding the default behavior of Strapi's core functionalities and plugins. Any customizations to the admin panel or modifications to plugin logic are placed here. -
.env
: An environment variables file where you can store sensitive or environment-specific settings, such as database credentials, API keys, or other configuration options that shouldn't be hard-coded into your application files. -
public/
: This directory is meant for static files that need to be publicly accessible. Files placed here can be accessed directly via the web server without any processing by Strapi. -
node_modules/
: Houses all the npm packages and dependencies your project needs, installed based on thepackage.json
file. -
package.json
: Defines the project dependencies, scripts, and general metadata about the project. This includes scripts for starting the server, installing dependencies, and other command-line tasks.
Our project leverages the Strapi Documentation plugin to automatically generate comprehensive API documentation. This documentation provides an in-depth look at all available API endpoints, including request methods, expected parameters, and response formats, facilitating easier integration and use of the API.
To access the API documentation:
-
Ensure the documentation plugin is installed and configured:
- The Documentation plugin should already be part of your Strapi project. If it's not, you can add it by running
npm install @strapi/plugin-documentation
and restarting your Strapi server.
- The Documentation plugin should already be part of your Strapi project. If it's not, you can add it by running
-
Navigate to the documentation URL:
- Once the plugin is installed, Strapi automatically generates the documentation and serves it at a URL path relative to your API's base URL. By default, this path is
/documentation
.
- Once the plugin is installed, Strapi automatically generates the documentation and serves it at a URL path relative to your API's base URL. By default, this path is
-
Using Swagger UI:
- The Strapi Documentation plugin utilizes Swagger UI for presenting the documentation. When you navigate to the documentation URL, you will see a Swagger interface that allows you to explore different API endpoints, their request methods, parameters, and responses. You can also try out API calls directly from this interface.
-
Customizing your documentation:
- The Documentation plugin offers customization options to tailor the generated documentation to your needs. You can configure things like the documentation's title, description, and version, as well as more detailed settings for each endpoint. These customizations can be made within the plugin's configuration file.
- Automated documentation: Automatically generates up-to-date documentation for your API, saving time and reducing errors.
- Interactive exploration: Allows developers to interact with the API directly from the documentation, facilitating testing and integration.
- Customizable: Offers options to customize the documentation to fit the branding and specific needs of your project.
Remember to regularly update your documentation as you modify your API to ensure that it accurately reflects the latest changes and capabilities.