2019-05-06 16:17:16 +02:00
# Routing
2019-10-09 12:32:58 +02:00
## Concept
`./api/**/config/routes.json` files define all available endpoints for the clients.
2020-03-22 13:28:03 -04:00
By default, Strapi generates endpoints for all your Content Types. More information is in the [Content API ](../content-api/api-endpoints.md ) documentation.
2019-05-06 16:17:16 +02:00
## How to create a route?
You have to edit the `routes.json` file in one of your APIs folders (`./api/**/config/routes.json` ) and manually add a new route object into the `routes` array.
**Path —** `./api/**/config/routes.json` .
2019-09-24 17:12:23 +02:00
2019-05-06 16:17:16 +02:00
```json
{
"routes": [
{
"method": "GET",
2019-09-24 17:12:23 +02:00
"path": "/restaurants",
2019-10-09 12:32:58 +02:00
"handler": "Restaurant.find",
"config": {
"policies": []
}
2019-05-06 16:17:16 +02:00
},
{
"method": ["POST", "PUT"],
2019-09-24 17:12:23 +02:00
"path": "/restaurants/:id",
2019-10-09 12:32:58 +02:00
"handler": "Restaurant.createOrUpdate",
"config": {
"policies": []
}
2019-05-06 16:17:16 +02:00
},
{
"method": "POST",
2019-09-24 17:12:23 +02:00
"path": "/restaurants/:id/reservation",
"handler": "Restaurant.reservation",
2019-05-06 16:17:16 +02:00
"config": {
"policies": ["isAuthenticated", "hasCreditCard"]
}
}
]
}
```
2020-02-08 16:44:04 +01:00
- `method` (string): Method or array of methods to hit the route (e.g. `GET` , `POST` , `PUT` , `HEAD` , `DELETE` , `PATCH` ).
- `path` (string): URL starting with `/` (e.g. `/restaurants` ).
- `handler` (string): Action to execute when the route is hit following this syntax `<Controller>.<action>` .
2019-05-06 16:17:16 +02:00
- `config`
2020-03-22 13:28:03 -04:00
- `policies` (array): Array of policy names or paths ([see more ](./policies.md ))
2019-05-06 16:17:16 +02:00
## Dynamic parameters
The router used by Strapi allows you to create dynamic routes where you can use parameters and simple regular expressions. These parameters will be exposed in the `ctx.params` object. For more details, please refer to the [PathToRegex ](https://github.com/pillarjs/path-to-regexp ) documentation.
```json
{
"routes": [
{
"method": "GET",
2019-09-24 17:12:23 +02:00
"path": "/restaurants/:category/:id",
2019-10-09 12:32:58 +02:00
"handler": "Restaurant.findOneByCategory",
"config": {
"policies": []
}
2019-05-06 16:17:16 +02:00
},
{
"method": "GET",
2019-09-24 17:12:23 +02:00
"path": "/restaurants/:region(\\d{2}|\\d{3})/:id", // Only match when the first parameter contains 2 or 3 digits.
2019-10-09 12:32:58 +02:00
"handler": "Restaurant.findOneByRegion",
"config": {
"policies": []
}
2019-05-06 16:17:16 +02:00
}
]
}
```
2020-04-01 11:48:38 +02:00
### Example
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
Route definition with URL params
2019-05-06 16:17:16 +02:00
```json
{
"routes": [
{
"method": "GET",
2020-04-01 11:48:38 +02:00
"path": "/restaurants/:id",
"handler": "Restaurant.findOne",
2019-10-09 12:32:58 +02:00
"config": {
"policies": []
}
2019-05-06 16:17:16 +02:00
}
]
}
```
2020-04-01 11:48:38 +02:00
Get the URL param in the controller
```js
module.exports = {
findOne: async ctx => {
// const id = ctx.params.id;
const { id } = ctx.params;
return id;
},
};
```