Custom route always 404

I am trying to create a custom route like this

export default {
  routes: [
    {
      method: 'GET',
      path: '/commands/get-points',
      handler: 'command.getPoints',
    },
  ],
}

I also enabled public access as you can see on the screenshot.
But this route always gives me a 404 response !

This topic has been created from a Discord post (1266325235628507157) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

whats your controller look like

here it is

import { factories } from '@strapi/strapi'

export default factories.createCoreController('api::command.command', ({ strapi }) => ({
  getPoints() {
    return 'oh yes'
}))

I have created a ticket here

try

async getPoints(ctx) {
  ctx.send({
    data: 'test',
  });
}

I tried but it did not work. I must mention that I have other custom routes on GET method that work, and they have been written the same way as I did for this command object.

Error is the error I get from Strapi:

{
  "data": null,
  "error": {
    "status": 404,
    "name": "NotFoundError",
    "message": "Not Found",
    "details": {}
  }
}

and you are sure you are hitting the right url? :smile:

yup :crazy_face: hitting http://localhost:1337/api/commands/get-points

hmm well i just setup that same endpoint in my project and had no issues

you running strapi in develop mode?

also do you really need all of those core api methods, or do you just want the getPoints method?

yes, I am running strapi develop.
I need the find and findOne yes.

hmm well i just setup that same endpoint in my project and had no issues
well yes I have no problem setting up other custom routes, but since today for new GET custom routes, I just can’t get them to work. Do you know where I could dive deeper to find something ?

oh i do have one difference, i set auth to false in my route

export default {
  routes: [
    {
      method: "GET",
      path: "/commands/get-points",
      handler: "command.getPoints",
      config: {
        auth: false,
      },
    },
  ],
};

doubt thats your issue though, figure it would throw a 401 instead of 404

yup I tried to add this config field, but without success.

What is weird is that when I only allow my “/commands/point” endpoint on public role, I get 403: I need to allow findOne to get 404. It seems that this GET route enters in conflict with the classic findOne route

<@604068445499162664> I have found the solution, which is a weird behaviour:

The routes are loaded for each file located in /routes for a particular object, in an alphabetical order. My order is:

<@604068445499162664> I have found the solution, which is a weird behaviour:

The routes seem to be loaded for each file located in /routes for a particular object, in an alphabetical order. My order is:

/routes
    command.ts
    custom-routes.ts

So I guess the core routes are first loaded, and then my custom routes defined in custom-routes.ts. I don’t understand why, but my GET route defined in custom-routes.ts is replaced by the findOne route.

When I change the order of the files to be something like this:

/routes
    a-custom-routes.ts
    command.ts

Then my custom route is recognized and works. I can’t explain why this is behaving like this, but here it is. So it seems that the general rule would be to always have one’s custom routes file before the core file ones.

1 Like

I have created a ticket for Strapi, hope this will be prioritized: Order of route files prevents custom routes from working · Issue #20887 · strapi/strapi · GitHub

the docs explain to prefix your custom route with 01-custom-route.ts for that reason :wink: