Skip to content

[cds^9] cds.middlewares.after + PATCH vs. PUT vs. Replace + Base Protocol Adapter #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
21 changes: 16 additions & 5 deletions node.js/cds-serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,31 @@ srv/cat-service.js #> service implementation used by default
For each service served at a certain protocol, the framework registers a configurable set of express middlewares by default like so:

```js
app.use (cds.middlewares.before, protocol_adapter)
app.use (cds.middlewares.before, protocol_adapter, cds.middlewares.after)
```

The standard set of middlewares uses the following order:

```js
cds.middlewares.before = [
context(), // provides cds.context
trace(), // provides detailed trace logs when DEBUG=trace
auth(), // provides req.user & tenant
context(), // provides cds.context
trace(), // provides detailed trace logs when DEBUG=trace
auth(), // provides req.user & tenant
ctx_auth(), // propagates auth results to cds.context
ctx_model(), // fills in cds.context.model
ctx_model(), // fills in cds.context.model
]
```

```js
cds.middlewares.after = [
cds_error_handler(), // provides final error handling
]
```

::: tip
In order for a custom error middleware to be invoked, it must be registered _before_ the built-in `cds_error_handler`. That is, it must be added, for example, via `cds.middlewares.after.unshift()`.
:::

::: warning _Be aware of the interdependencies of middlewares_ <!-- -->
_ctx_model_ requires that _cds.context_ middleware has run before.
_ctx_auth_ requires that _authentication_ has run before.
Expand Down
Loading