Part 35: Expanding Our Shop with Strapi: A Pre-Populated Backend for Next.js

[Pages] Next Shop: Setup

[Pages] Next Shop: Setup

In our previous exploration of Strapi, we set up a basic backend with a couple of products to understand how this powerful Headless CMS can serve our shop. Now, we're ready to take a deeper dive by integrating a more comprehensive backend, complete with pre-populated data, into our Next.js application. This step is crucial for developing a fully functional shop, complete with a variety of products ready for display.

Setting Up the Pre-Populated Strapi Backend

To streamline our development process, I've prepared a Strapi project with pre-existing data that you can download and use. This project will save you the time and effort of manually entering each product.

Download and Unpack the Project

  1. Download the Backend: Start by downloading the "next-shop-backend" ZIP file from the resources provided here. This file contains the Strapi project with all necessary configurations and data.

2MB
Open
  1. Extract the Files: Save the ZIP file into your Projects folder, where your Next.js project resides. Then, extract it by double-clicking, which will create a "next-shop-backend" folder.

  2. Open in Code Editor: Navigate to this folder and open it in a code editor like Visual Studio Code. You'll see a familiar project structure, similar to what we explored before, but this time with more data.

Install and Build the Project

Before the server, we need to install necessary dependencies and build the project:

  1. Install Dependencies: Run npm install to set up the project dependencies.

  2. Build the Project: Since the code doesn't require modifications, execute npm run build to create an optimized build.

  3. Start the Server: Finally, run npm start to launch the Strapi server. This command will start the server, ready to handle requests.

Accessing the Strapi Dashboard

Once the server is running, access the admin pages via the provided URL. Log in using the pre-configured admin account with the email [email protected] and password Admin123. This brings you to the Strapi dashboard, where you can manage and explore the pre-populated data.

Exploring the Pre-Populated Collections

In this setup, several collection types are already defined:

  • Users: This collection includes sample users such as "Alice" and "Bob", which will be useful when implementing authentication features.

  • Cart Items: This collection is designed to handle cart functionalities, allowing users to add products to their shopping cart.

  • Products: This is the primary collection we'll focus on. It contains a list of items available for sale, complete with titles, descriptions, prices, and images.

Example Product: Aloe Vera Plant

One of the products included is the "Aloe Vera" plant, showcasing essential fields such as Title, Description, Price, and Picture. Having this data pre-loaded allows us to focus on integrating and displaying it within our Next.js application.

Fetching Product Data via REST API

The Strapi backend is configured to expose product data through a REST API, which our Next.js application can access. To test this:

  1. Use a REST Client: In your code editor, create a new file and set its type to "http". This allows you to write and send HTTP requests.

  2. Send a GET Request: Use a GET request to the Strapi server URL followed by /products. If configured correctly, you'll receive a 200 OK response, along with a JSON array of products.

  3. Understanding the Response: The response includes product details, including a "picture" field with multiple properties. We'll explore how to handle these when displaying images on the website.

Moving Forward with Next.js

With the Strapi backend fully set up and populated with data, we can now shift our focus back to the Next.js application. The next step involves fetching this product data and integrating it into our shop pages, enabling us to display product information dynamically.

By having this robust backend in place, we're well-equipped to build a feature-rich shop that can effectively showcase products and engage users. Stay tuned as we continue this journey, diving into the integration of our frontend and backend systems.

Last updated