docs: added a section on middlewares and trailing backslashes (#8942)

This commit is contained in:
Shahed Nasser
2024-09-03 17:32:36 +03:00
committed by GitHub
parent c42d66f239
commit 46a70eaf5b
4 changed files with 50 additions and 2 deletions
@@ -55,6 +55,48 @@ In the example above, you define a middleware that logs the message `Received a
---
## Request URLs with Trailing Backslashes
A middleware whose `matcher` pattern doesn't end with a backslash won't be applied for requests to URLs with a trailing backslash.
For example, consider you have the following middleware:
```ts
import { defineMiddlewares } from "@medusajs/medusa"
import type {
MedusaNextFunction,
MedusaRequest,
MedusaResponse,
} from "@medusajs/medusa"
export default defineMiddlewares({
routes: [
{
matcher: "/custom",
middlewares: [
(
req: MedusaRequest,
res: MedusaResponse,
next: MedusaNextFunction
) => {
console.log("Received a request!")
next()
},
],
},
],
})
```
If you send a request to `http://localhost:9000/custom`, the middleware will run.
However, if you send a request to `http://localhost:9000/custom/`, the middleware won't run.
In general, avoid adding trailing backslashes when sending requests to API routes.
---
## Test the Middleware
To test the middleware:
+1 -1
View File
@@ -61,7 +61,7 @@ export const generatedEditDates = {
"app/advanced-development/admin/tips/page.mdx": "2024-08-05T13:20:34+03:00",
"app/advanced-development/api-routes/cors/page.mdx": "2024-07-25T17:14:06+02:00",
"app/advanced-development/admin/ui-routes/page.mdx": "2024-08-06T09:44:22+02:00",
"app/advanced-development/api-routes/middlewares/page.mdx": "2024-07-25T17:14:06+02:00",
"app/advanced-development/api-routes/middlewares/page.mdx": "2024-09-02T13:52:00.236Z",
"app/advanced-development/modules/isolation/page.mdx": "2024-07-04T17:26:03+03:00",
"app/advanced-development/data-models/configure-properties/page.mdx": "2024-07-04T17:26:03+03:00",
"app/advanced-development/data-models/index/page.mdx": "2024-07-04T17:26:03+03:00",