Hi, In Strapi 3 all routes were handled in the routes.json. When a new content type was added, all core routes were added to this routes.json automatically. When there was a need for an additional route, I could duplicate on of the existing routes and modify it.
Now with Strapi 4 the routes are not added to a routes file anymore, instead only a createCoreRouter() function is called which dynamically creates all core routes.
For modifying the routes, the Strapi 4 Documentation lists two ways:
First, modifying the core routes by adding a second paramater to this createCoreRouter() function.
Second, using a custom router and adding all routes manually.
However, I would like to use a combination of both. I want to use all core routes but additionally some custom ones e.g. “deleteAllExpired”.
Is there a way to create a custom additional route, without rebuilting the core routes manually? e.g. extending the createCoreRouter() function with some custom routes? I did not find an example on how to do this.
This works where the path does not include the content-type, which is not really combining the routes as the OP specifies but creating a separate route, is there a way to combine/extend routes like in v3?
E.g. path: "/the-content-types/my-custom-route" won’t work resulting in a 404 I guess because of the way createCoreRouter creates defined routes whereas path: "/foo/my-custom-route" or path: "/my-custom-route" will resolve. A feature where createCoreRouter could be passed additional routes could be nice
Very useful, thanks! Just a follow-up question, how will I do the exact same thing but with the user api? That is, I want to add some extra routes to the user api. But I can’t seem to apply your answer because there’s no user folder under api, so there’s also no routes > user.js file where I could put your code snippet.
This was very interesting read. I was wondering if there is a way to use similar pattern to be able to override existing routes vs creating extra ones.
I looked in the docs, but was not able to find a good example of overriding a route, just a way to configure them.
So I tried this and it worked. Not the best implementation, just experimenting based on the above example.
Hi ! I do not get what I am doing wrong. but it always returns 404
I’ve done everything the same as in tutor above but it still returns 404 /
Any Idea?
P.S. In roles settings I set checkbox of the API endpoint to true as well .