From 0b2747c6af6d7ca07e8a0e5deefbc18496f63ebd Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Mon, 18 Oct 2021 18:54:46 +0200 Subject: [PATCH 01/13] Update routes file in backend customization --- docs/.vuepress/config.js | 2 +- .../backend-customization/routes.md | 170 ++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 docs/developer-docs/latest/development/backend-customization/routes.md diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 1ff86049fc..35fc3f3527 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -152,7 +152,7 @@ const sidebar = { title: 'Backend customization', collapsable: true, children: [ - ['/developer-docs/latest/development/backend-customization/routing.md', 'Routing'], + ['/developer-docs/latest/development/backend-customization/routes.md', 'Routes'], ['/developer-docs/latest/development/backend-customization/policies.md', 'Policies'], ['/developer-docs/latest/development/backend-customization/controllers.md', 'Controllers'], ['/developer-docs/latest/development/backend-customization/requests-responses.md', 'Requests & Responses'], diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md new file mode 100644 index 0000000000..fb7211d813 --- /dev/null +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -0,0 +1,170 @@ +--- +title: Routes - Strapi Developer Documentation +description: … +sidebarDepth: 3 +--- + + + +# Routes + +Strapi runs an HTTP server. Requests sent to Strapi on any URL are handled by routes. By default, Strapi generates routes for all the content-types (see [REST API documentation](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md)). But just like [all the other parts of the Strapi backend](/developer-docs/latest/development/backend-customization.md), routes can be customized. New routes [can be added](#implementation) and configured: + +- with [policies](#policies), which are a way to block access to a route, +- and with [middlewares](#middlewares), which are a way to control and change the request flow and the request itself. + +Once a route exists, reaching it executes some code handled by a controller (see [controllers](/developer-docs/latest/development/backend-customization/controllers.md) documentation). + +## Implementation + +Implementing a new route consists in defining it in a router file within the `.src/api/[apiName]/routes` folder (see [project structure](/developer-docs/latest/setup-deployment-guides/file-structure.md)). + +A router file consists in an array of objects, each object being a route with the following parameters: + +| Parameter | Description | Type | +| -------------------------- | -------------------------------------------------------------------------------- | -------- | +| `method` | Method associated to the route (i.e. `GET`, `POST`, `PUT`, `DELETE` or `PATCH`) | `String` | +| `path` | URL to reach | `String` | +| `handler` | Function to execute when the route is reached.
Should follow this syntax: `.` | `String` | +| `config`

_Optional_ | Configuration to handle [policies](policies) and [middlewares](middlewares) applied to the route

| `Object` | + +**Example**: To handle any GET request on the `/articles` URL and call the `actionName` function on the `controllerName` [controller](#controllers): + +```js +// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') + +module.exports = { + routes: [ + { + method: 'GET', + path: '/articles', + handler: 'controllerName.actionName', + }, + ], +}; +``` + +The router used by Strapi allows the creation of dynamic routes, using 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. + +::: details Example of routes using URL parameters and regular expressions +```js +// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') + +module.exports = { + routes: [ + { + "method": "GET", + "path": "/restaurants/:category/:id", + "handler": "Restaurant.findOneByCategory", + "config": { + "policies": [] + } + }, + { + "method": "GET", + "path": "/restaurants/:region(\\d{2}|\\d{3})/:id", // Only match when the first parameter contains 2 or 3 digits. + "handler": "Restaurant.findOneByRegion", + "config": { + "policies": [] + } + } + ] +} +``` + +::: + +## Configuration + +The optional configuration for a route is defined in its `config` object, which can be used to handle [policies](#policies) and [middlewares](#middlewares) or to [make the route public](#public-routes). + +### Policies + +Policies can be added to a route configuration: + +- by pointing to a policy registered in `./src/policies`, with or without passing a custom configuration +- or by declaring the policy implementation directly, as a function that takes [Koa's context](https://koajs.com/#context) (`ctx`) and the `strapi` instance as arguments + + +```js +// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') + +module.exports = { + routes: [ + { + method: 'GET', + path: '/articles', + handler: 'controllerName.actionName', + config: { + policies: [ + 'policy-name', // point to a registered policy + { name: 'policy-name', config: {} }, // point to a registered policy with some custom configuration + // pass a policy implementation directly + (ctx, { strapi }) => { + return true; + }, + ], + }, + }, + ], +}; +``` + +### Middlewares + +Middlewares can be added to a route configuration: + +- by pointing to a middleware registered in `./src/middlewares`, with or without passing a custom configuration +- or by declaring the middleware implementation directly, as a function that takes [Koa's context](https://koajs.com/#context) (`ctx`) and the `strapi` instance as arguments: + + +```js +// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') + +module.exports = { + routes: [ + { + method: 'GET', + path: '/articles', + handler: 'controllerName.actionName', + config: { + middlewares: [ + 'middleware-name', // point to a registered middleware + { name: 'middleware-name', config: {} }, // point to a registered middleware with some custom configuration + // pass a middleware implementation directly + (ctx, next) => { + return next(); + }, + ], + }, + }, + ], +}; +``` + +### Public routes + +By default, routes are protected by Strapi's authentication system, which is based on API tokens and/or the use of a plugin such as the [Users & Permissions plugin](/user-docs/latest/plugins/strapi-plugins.html#users-permissions-plugin). + +In some scenarios, it can be useful to have a route publicly available, and control the access outside of the normal Strapi authentication system. This can be achieved by setting the `auth` configuration parameter of a route to `false`: + +```js +// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') + +module.exports = { + routes: [ + { + method: 'GET', + path: '/articles', + handler: 'controllerName.actionName', + config: { + auth: false, + }, + }, + ], +}; +``` + +*** + + From f04746d3dce733bb9f4e0d33ef6994327162292d Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 15:46:56 +0200 Subject: [PATCH 02/13] Improve routes content and wording --- .../backend-customization/routes.md | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index fb7211d813..664afa75b8 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -8,7 +8,7 @@ sidebarDepth: 3 # Routes -Strapi runs an HTTP server. Requests sent to Strapi on any URL are handled by routes. By default, Strapi generates routes for all the content-types (see [REST API documentation](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md)). But just like [all the other parts of the Strapi backend](/developer-docs/latest/development/backend-customization.md), routes can be customized. New routes [can be added](#implementation) and configured: +Requests sent to Strapi on any URL are handled by routes. By default, Strapi generates routes for all the content-types (see [REST API documentation](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md)). But just like [all the other parts of the Strapi backend](/developer-docs/latest/development/backend-customization.md), routes can be customized. New routes [can be added](#implementation) and configured: - with [policies](#policies), which are a way to block access to a route, - and with [middlewares](#middlewares), which are a way to control and change the request flow and the request itself. @@ -24,11 +24,11 @@ A router file consists in an array of objects, each object being a route with th | Parameter | Description | Type | | -------------------------- | -------------------------------------------------------------------------------- | -------- | | `method` | Method associated to the route (i.e. `GET`, `POST`, `PUT`, `DELETE` or `PATCH`) | `String` | -| `path` | URL to reach | `String` | +| `path` | Path to reach, starting with a forward-leading slash (e.g. `/articles`)| `String` | | `handler` | Function to execute when the route is reached.
Should follow this syntax: `.` | `String` | -| `config`

_Optional_ | Configuration to handle [policies](policies) and [middlewares](middlewares) applied to the route

| `Object` | +| `config`

_Optional_ | Configuration to handle [policies](policies), [middlewares](middlewares) and [public availability](#public-routes) for the route

| `Object` | -**Example**: To handle any GET request on the `/articles` URL and call the `actionName` function on the `controllerName` [controller](#controllers): +**Example**: Handle any `GET` request on the `/articles` path by calling the `actionName` function from the `controllerName` [controller](/developer-docs/latest/development/backend-customization/controllers.md): ```js // path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') @@ -52,7 +52,7 @@ The router used by Strapi allows the creation of dynamic routes, using parameter module.exports = { routes: [ - { + { // Path defined with a URL parameter "method": "GET", "path": "/restaurants/:category/:id", "handler": "Restaurant.findOneByCategory", @@ -60,7 +60,7 @@ module.exports = { "policies": [] } }, - { + { // Path defined with a regular expression "method": "GET", "path": "/restaurants/:region(\\d{2}|\\d{3})/:id", // Only match when the first parameter contains 2 or 3 digits. "handler": "Restaurant.findOneByRegion", @@ -80,7 +80,7 @@ The optional configuration for a route is defined in its `config` object, which ### Policies -Policies can be added to a route configuration: +[Policies](/developer-docs/latest/development/backend-customization/policies.md) can be added to a route configuration: - by pointing to a policy registered in `./src/policies`, with or without passing a custom configuration - or by declaring the policy implementation directly, as a function that takes [Koa's context](https://koajs.com/#context) (`ctx`) and the `strapi` instance as arguments @@ -112,7 +112,7 @@ module.exports = { ### Middlewares -Middlewares can be added to a route configuration: +[Middlewares](/developer-docs/latest/development/backend-customization/middlewares.md) can be added to a route configuration: - by pointing to a middleware registered in `./src/middlewares`, with or without passing a custom configuration - or by declaring the middleware implementation directly, as a function that takes [Koa's context](https://koajs.com/#context) (`ctx`) and the `strapi` instance as arguments: @@ -144,9 +144,11 @@ module.exports = { ### Public routes -By default, routes are protected by Strapi's authentication system, which is based on API tokens and/or the use of a plugin such as the [Users & Permissions plugin](/user-docs/latest/plugins/strapi-plugins.html#users-permissions-plugin). +By default, routes are protected by Strapi's authentication system, which is based on [API tokens]() or the use of a plugin such as the [Users & Permissions plugin](/user-docs/latest/plugins/strapi-plugins.html#users-permissions-plugin). + + -In some scenarios, it can be useful to have a route publicly available, and control the access outside of the normal Strapi authentication system. This can be achieved by setting the `auth` configuration parameter of a route to `false`: +In some scenarios, it can be useful to have a route publicly available and control the access outside of the normal Strapi authentication system. This can be achieved by setting the `auth` configuration parameter of a route to `false`: ```js // path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') @@ -166,5 +168,3 @@ module.exports = { ``` *** - - From b917212a126e14b4f4ed1704d8647acca5a3a0b9 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 17:36:42 +0200 Subject: [PATCH 03/13] =?UTF-8?q?Fix=20syntax=20(JSON=20=E2=86=92=20=20JS)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend-customization/routes.md | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index 664afa75b8..8ab114c370 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -53,20 +53,14 @@ The router used by Strapi allows the creation of dynamic routes, using parameter module.exports = { routes: [ { // Path defined with a URL parameter - "method": "GET", - "path": "/restaurants/:category/:id", - "handler": "Restaurant.findOneByCategory", - "config": { - "policies": [] - } + method: 'GET', + path: '/restaurants/:category/:id', + handler: 'Restaurant.findOneByCategory', }, { // Path defined with a regular expression - "method": "GET", - "path": "/restaurants/:region(\\d{2}|\\d{3})/:id", // Only match when the first parameter contains 2 or 3 digits. - "handler": "Restaurant.findOneByRegion", - "config": { - "policies": [] - } + method: 'GET', + path: '/restaurants/:region(\\d{2}|\\d{3})/:id', // Only match when the first parameter contains 2 or 3 digits. + handler: 'Restaurant.findOneByRegion', } ] } From 82bca89fa392727bd4f1da6322cbaf906a825a31 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 17:37:08 +0200 Subject: [PATCH 04/13] Delete routing.md (Now using routes) --- .../backend-customization/routing.md | 115 ------------------ 1 file changed, 115 deletions(-) delete mode 100644 docs/developer-docs/latest/development/backend-customization/routing.md diff --git a/docs/developer-docs/latest/development/backend-customization/routing.md b/docs/developer-docs/latest/development/backend-customization/routing.md deleted file mode 100644 index da482a2841..0000000000 --- a/docs/developer-docs/latest/development/backend-customization/routing.md +++ /dev/null @@ -1,115 +0,0 @@ ---- -title: -description: ---- - - - -# Routing - -`./api/**/config/routes.json` files define all available endpoints for the clients. - -By default, Strapi generates endpoints for all your Content Types. More information is in the [Content API](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md#api-endpoints) documentation. - -## 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`. - -```json -{ - "routes": [ - { - "method": "GET", - "path": "/restaurants", - "handler": "Restaurant.find", - "config": { - "policies": [] - } - }, - { - "method": "PUT", - "path": "/restaurants/bulkUpdate", - "handler": "Restaurant.bulkUpdate", - "config": { - "policies": [] - } - }, - { - "method": "POST", - "path": "/restaurants/:id/reservation", - "handler": "Restaurant.reservation", - "config": { - "policies": ["is-authenticated", "has-credit-card"] - } - } - ] -} -``` - -- `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 `.`. -- `config` - - `policies` (array): Array of policy names or paths ([see more](/developer-docs/latest/development/backend-customization/policies.md)) - -::: tip -You can exclude the entire `config` object if you do not want the route to be checked by the [Users & Permissions plugin](/developer-docs/latest/plugins/users-permissions.md). -::: - -## 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", - "path": "/restaurants/:category/:id", - "handler": "Restaurant.findOneByCategory", - "config": { - "policies": [] - } - }, - { - "method": "GET", - "path": "/restaurants/:region(\\d{2}|\\d{3})/:id", // Only match when the first parameter contains 2 or 3 digits. - "handler": "Restaurant.findOneByRegion", - "config": { - "policies": [] - } - } - ] -} -``` - -Example: Route definition with URL params: - -```json -{ - "routes": [ - { - "method": "GET", - "path": "/restaurants/:id", - "handler": "Restaurant.findOne", - "config": { - "policies": [] - } - } - ] -} -``` - -Get the URL param in the controller - -```js -module.exports = { - findOne: async ctx => { - // const id = ctx.params.id; - const { id } = ctx.params; - return id; - }, -}; -``` From ca993b25103529fe576d5aaa7b462a53c46c5d29 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 17:37:22 +0200 Subject: [PATCH 05/13] Delete backend-customization.md --- docs/developer-docs/latest/development/backend-customization.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 docs/developer-docs/latest/development/backend-customization.md diff --git a/docs/developer-docs/latest/development/backend-customization.md b/docs/developer-docs/latest/development/backend-customization.md deleted file mode 100644 index e69de29bb2..0000000000 From 89410c018b8a4eb44e187f38b768b4138e11ea3b Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 22:47:42 +0200 Subject: [PATCH 06/13] =?UTF-8?q?Consists=20in=20=E2=86=92=20=20consists?= =?UTF-8?q?=20of?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: DMehaffy --- .../latest/development/backend-customization/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index 8ab114c370..ef2a031f04 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -19,7 +19,7 @@ Once a route exists, reaching it executes some code handled by a controller (see Implementing a new route consists in defining it in a router file within the `.src/api/[apiName]/routes` folder (see [project structure](/developer-docs/latest/setup-deployment-guides/file-structure.md)). -A router file consists in an array of objects, each object being a route with the following parameters: +A router file consists of an array of objects, each object being a route with the following parameters: | Parameter | Description | Type | | -------------------------- | -------------------------------------------------------------------------------- | -------- | From e332d6f1fbe42b97f2e2df46481bdef90cb7f96a Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 22:48:04 +0200 Subject: [PATCH 07/13] Add missing forward slash in path Co-authored-by: DMehaffy --- .../latest/development/backend-customization/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index ef2a031f04..c7c77bf226 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -81,7 +81,7 @@ The optional configuration for a route is defined in its `config` object, which ```js -// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') +// path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') module.exports = { routes: [ From 8b6945d844ed6ca2b2c405c556a535a5e1072b7e Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 22:50:00 +0200 Subject: [PATCH 08/13] Add missing forward slash in path Co-authored-by: DMehaffy --- .../latest/development/backend-customization/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index c7c77bf226..e861fb6a3f 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -31,7 +31,7 @@ A router file consists of an array of objects, each object being a route with th **Example**: Handle any `GET` request on the `/articles` path by calling the `actionName` function from the `controllerName` [controller](/developer-docs/latest/development/backend-customization/controllers.md): ```js -// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') +// path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') module.exports = { routes: [ From 76c931a5a74f5ac61a53c0fd0c7fbed5e00bdb99 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 22:51:34 +0200 Subject: [PATCH 09/13] =?UTF-8?q?Add=20another=20missing=20forward=20slash?= =?UTF-8?q?=20in=20path=20=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: DMehaffy --- .../latest/development/backend-customization/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index e861fb6a3f..cbc142c5dd 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -113,7 +113,7 @@ module.exports = { ```js -// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') +// path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') module.exports = { routes: [ From 10f5ab64665b1cc8b640d41789038e039713e421 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 23:08:50 +0200 Subject: [PATCH 10/13] =?UTF-8?q?Add=20another=20missing=20forward=20slash?= =?UTF-8?q?=20in=20path,=20again=20=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: DMehaffy --- .../latest/development/backend-customization/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index cbc142c5dd..b9871457ae 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -48,7 +48,7 @@ The router used by Strapi allows the creation of dynamic routes, using parameter ::: details Example of routes using URL parameters and regular expressions ```js -// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') +// path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') module.exports = { routes: [ From d7f09a92473193c3dcdb1d657ce0414bfb8ff2c8 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 19 Oct 2021 23:09:28 +0200 Subject: [PATCH 11/13] =?UTF-8?q?Add=20another=20missing=20forward=20slash?= =?UTF-8?q?=20=F0=9F=98=85=20=20=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: DMehaffy --- .../latest/development/backend-customization/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index b9871457ae..2aa5619c05 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -145,7 +145,7 @@ By default, routes are protected by Strapi's authentication system, which is bas In some scenarios, it can be useful to have a route publicly available and control the access outside of the normal Strapi authentication system. This can be achieved by setting the `auth` configuration parameter of a route to `false`: ```js -// path: .src/api/[apiName]/routes/[routerName].js (e.g '.src/api/blog/routes/articles.js') +// path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') module.exports = { routes: [ From 56e4dbe51c5071009facc723c7b5bdb777e7978a Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Thu, 21 Oct 2021 08:55:53 +0200 Subject: [PATCH 12/13] Remove question comment, no `next` in policies --- .../latest/development/backend-customization/routes.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index 2aa5619c05..fe3d7f802c 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -78,7 +78,6 @@ The optional configuration for a route is defined in its `config` object, which - by pointing to a policy registered in `./src/policies`, with or without passing a custom configuration - or by declaring the policy implementation directly, as a function that takes [Koa's context](https://koajs.com/#context) (`ctx`) and the `strapi` instance as arguments - ```js // path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') From 97c8691cc44d44a2da74b5dbea4ea43b023a3eeb Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Tue, 2 Nov 2021 17:07:01 +0100 Subject: [PATCH 13/13] =?UTF-8?q?Update=20based=20on=20M=C3=A9gane's=20fee?= =?UTF-8?q?dback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../development/backend-customization/routes.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/developer-docs/latest/development/backend-customization/routes.md b/docs/developer-docs/latest/development/backend-customization/routes.md index fe3d7f802c..a3f2bb1963 100644 --- a/docs/developer-docs/latest/development/backend-customization/routes.md +++ b/docs/developer-docs/latest/development/backend-customization/routes.md @@ -8,7 +8,7 @@ sidebarDepth: 3 # Routes -Requests sent to Strapi on any URL are handled by routes. By default, Strapi generates routes for all the content-types (see [REST API documentation](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md)). But just like [all the other parts of the Strapi backend](/developer-docs/latest/development/backend-customization.md), routes can be customized. New routes [can be added](#implementation) and configured: +Requests sent to Strapi on any URL are handled by routes. By default, Strapi generates routes for all the content-types (see [REST API documentation](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md)). Routes can be [added](#implementation) and configured: - with [policies](#policies), which are a way to block access to a route, - and with [middlewares](#middlewares), which are a way to control and change the request flow and the request itself. @@ -28,7 +28,9 @@ A router file consists of an array of objects, each object being a route with th | `handler` | Function to execute when the route is reached.
Should follow this syntax: `.` | `String` | | `config`

_Optional_ | Configuration to handle [policies](policies), [middlewares](middlewares) and [public availability](#public-routes) for the route

| `Object` | -**Example**: Handle any `GET` request on the `/articles` path by calling the `actionName` function from the `controllerName` [controller](/developer-docs/latest/development/backend-customization/controllers.md): +Generic implementation example: + +To handle any `GET` request on the `/articles` path by calling the `actionName` function from the `controllerName` [controller](/developer-docs/latest/development/backend-customization/controllers.md), use the following code: ```js // path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js') @@ -44,7 +46,9 @@ module.exports = { }; ``` -The router used by Strapi allows the creation of dynamic routes, using 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. +
+ +The router used by Strapi allows the creation of dynamic routes, using parameters and 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. ::: details Example of routes using URL parameters and regular expressions ```js @@ -109,7 +113,6 @@ module.exports = { - by pointing to a middleware registered in `./src/middlewares`, with or without passing a custom configuration - or by declaring the middleware implementation directly, as a function that takes [Koa's context](https://koajs.com/#context) (`ctx`) and the `strapi` instance as arguments: - ```js // path: ./src/api/[apiName]/routes/[routerName].js (e.g './src/api/blog/routes/articles.js')