\n├─ event/
\n│ ├─ content-types/
\n│ │ ├─ schema.json
\n│ ├─ controllers/
\n│ │ ├─ event.js
\n│ ├─ routes/
\n│ │ ├─ event.js
\n│ │ ├─ me.js
\n│ ├─ services/
\n│ │ ├─ event.js
With the endpoint '/api/me':
\napi/event/routes/me.js
"use-strict";\n\nmodule.exports = {\n routes: [\n {\n method: "GET",\n path: "/me",\n handler: "event.me",\n },\n ],\n};\n
\nWith the endpoint '/api/events/me':
\napi/event/routes/me.js
"use-strict";\n\nmodule.exports = {\n routes: [\n {\n method: "GET",\n path: "/events/me",\n handler: "event.me",\n },\n ],\n};\n
\nEdit:
\nI think strapi confuses my custom endpoint 'events/me' with the core route endpoint for findOne which is 'events/:id'. So the '/me' part of my endpoint is probably considered the ':id', and as there is no id of 'me', I get a 404 error. Would there be a way to prioritize my custom route over the core route for findOne?
Reputation: 1
I just started using Strapi, so this is probably a silly question. But I was following a course and at one point I had a create a custom route in strapi. The course is using strapi v3 but I am using strapi v4. So I created a custom route following the docs: https://docs.strapi.io/developer-docs/latest/development/backend-customization/routes.html#creating-custom-routers
My route works perfectly fine when I set the path to "/me" but I get a 404 error when the path is set to "/events/me". Although this is a minor inconvenience, I would like to know if there is a way to set the path to "events/me".
Here is my folder structure and code:
api/
├─ event/
│ ├─ content-types/
│ │ ├─ schema.json
│ ├─ controllers/
│ │ ├─ event.js
│ ├─ routes/
│ │ ├─ event.js
│ │ ├─ me.js
│ ├─ services/
│ │ ├─ event.js
With the endpoint '/api/me':
api/event/routes/me.js
"use-strict";
module.exports = {
routes: [
{
method: "GET",
path: "/me",
handler: "event.me",
},
],
};
With the endpoint '/api/events/me':
api/event/routes/me.js
"use-strict";
module.exports = {
routes: [
{
method: "GET",
path: "/events/me",
handler: "event.me",
},
],
};
Edit:
I think strapi confuses my custom endpoint 'events/me' with the core route endpoint for findOne which is 'events/:id'. So the '/me' part of my endpoint is probably considered the ':id', and as there is no id of 'me', I get a 404 error. Would there be a way to prioritize my custom route over the core route for findOne?
Upvotes: 0
Views: 1083
Reputation: 1
Turns out it was a silly mistake. I am gonna leave the solution here for anyone to read. I went over the docs a few more times and learned that Koa (the backend framework strapi uses) processes routes alphabetically. So my simple solution was to just rename the custom router file from 'me.js' to '01-me.js'..
Upvotes: 0