Custom route & controller method - throws 404

System Information
  • 3.4.4:
  • Windows 10:
  • SqLite:
  • 10.16.0:
  • NPM Version:
  • Yarn Version:

Hi There

I’m a newbie to Strapi - trying to extend the API by making my own route and controller method:

routes.json:

    {
  "method": "GET",
  "path": "/observations/gallery",
  "handler": "observation.gallery",
  "config": {
    "policies": []
  }
}

and my own controller method:
observation.js

module.exports = {
async gallery(ctx) {
    let entities;

    entities = await strapi.query('observation').find();
    return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.observation }));
}

};

The controller method is visible and enabled in admin/settings/users-permissions/roles/

But I keep getting 404 when trying to call it by localhost:1337/observations/gallery.

Thanks in advance for any hints… :slight_smile:

/Thomas

1 Like

Aaah I got it- just moved the route up in the routes.json file - before /:id and just after /count.

Problem solved.

Exactly :slight_smile: The reason for this is Koa (Strapi’s backend framework it uses) match routes based on regex, and does so in order of the JSON file from top to bottom.

This means “/observations/gallery” looks like gallery is the :id :stuck_out_tongue:

1 Like