404 Not Found Error on Custom Strapi v4 Endpoint For Search Functionality

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

In Strapi, I have a table with various entries, and within this table, there is a field named code that contains a randomly generated number. My main goal is to locate a specific record using this code field. I need guidance on how to properly make a RESTful API request to fetch the data associated with a particular code.

For instance, when a user in my Flutter app makes a reservation, they receive this unique code. Later, they should be able to use this code through the Strapi API to retrieve their reservation details. Therefore, I need the endpoint to respond with the specific record when provided with the code as a query parameter.

I am encountering a persistent issue with a custom search endpoint in my Strapi v4 application. Unfortunately, I don’t have a background in Node.js, and the endpoint is intended to be consumed by a Flutter application. Here’s what’s happening:

Issue Description:

When I submit a GET request to my custom endpoint (https://example.com/api/reserves/search?code=12962), I receive a 404 Not Found response. There’s no error showing up in the console, only this log entry:

[2024-02-04 16:30:10] [2024-02-04 16:30:10.232] http: GET /api/reserves/search?code=12962 (7 ms) 404 Troubleshooting Done:

Ensured that the route and controller are correctly set up in the Strapi configuration. Verified that the code exists in the database and is being passed correctly in the query. Checked for any possible typos or misconfigurations in route definitions.

Here’s the code for the custom controller:

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::reserve.reserve', ({ strapi }) => ({
  async search(ctx) {
    try {
      const { code } = ctx.query;
      if (!code) {
        return ctx.badRequest('کد پیگیری مورد نیاز است.');
      }

      const entity = await strapi.entityService.findMany('api::reserve.reserve', {
        filters: { code },
      });

      if (!entity.length) {
        return ctx.notFound('هیچ رزروی با این کد پیگیری یافت نشد.');
      }

      return entity;
    } catch (err) {
      return ctx.internalServerError('خطا در پردازش درخواست');
    }
  },
}));

And the custom route definition:

module.exports = {
  routes: [
    {
      method: 'GET',
      path: '/reserves/search',
      handler: 'reserve.search',
      config: {
        auth: false,
      },
    },
  ],
};

I have placed the route definition in /app/src/api/reserve/routes/reserves.js.


Questions:

What might be the reason for the 404 Not Found response even though the route seems to be defined correctly? Is there a misconfiguration I might be overlooking in Strapi when setting up custom search endpoints? I would greatly appreciate any help or insights you can provide to resolve this issue.

Thank you in advance!

Most of all, I want to know how to make a custom controller and root for this.